Create Discount Code with Shopify API

 import uvicorn
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field, create_model
from typing import Optional, Dict, Any
import requests
import os

app = FastAPI()

# Environment variable for Shopify Admin API Token
shopify_admin_api_token = os.environ['SHOPIFY_ADMIN_API_TOKEN']

class PriceRuleBase(BaseModel):
    title: str = Field(default="Default Title")
    value: str = Field(default="-10.0")
    starts_at: str = Field(default=None)
    target_type: str = Field(default="line_item")
    target_selection: str = Field(default="all")
    allocation_method: str = Field(default="across")
    value_type: str = Field(default="fixed_amount")
    customer_selection: str = Field(default="all")

class AdditionalPriceRuleData(BaseModel):
    # This model will hold any additional fields the user wants to include in the price rule payload

About this template

The app allows users to create price rules and discount codes for their Shopify store using API. Users can provide a base set of data for the price rule, and optionally include additional fields to customize the rule further. The Shopify Admin API token is securely taken from environment variables, ensuring sensitive data is handled properly. Please set the `SHOPIFY_ADMIN_API_TOKEN` in the Env Secrets tab.

Introduction to the Shopify Discount Code Creation Template

Welcome to the Shopify Discount Code Creation Template! This template is designed to help you easily create price rules and discount codes for your Shopify store using the Shopify Admin API. With this template, you can provide a base set of data for the price rule and optionally include additional fields to customize the rule further. The process is secure and straightforward, as the Shopify Admin API token is taken from environment secrets within the Lazy Builder.

Getting Started

To begin using this template, simply click on "Start with this Template" in the Lazy Builder interface. This will pre-populate the code in the Lazy Builder interface, so you won't need to copy, paste, or delete any code.

Initial Setup: Adding Environment Secrets

Before you can use this template, you'll need to set up an environment secret for the Shopify Admin API token. Here's how to do it:

  • Log in to your Shopify Admin dashboard.
  • Go to the "Apps" section and create a new private app to obtain an API token.
  • Copy the Admin API token provided by Shopify.
  • In the Lazy Builder, navigate to the Environment Secrets tab.
  • Create a new secret with the key SHOPIFY_ADMIN_API_TOKEN and paste the API token you copied as the value.

Test: Deploying the App

Once you have set up the environment secret, you can deploy the app by pressing the "Test" button. This will begin the deployment of the app and launch the Lazy CLI.

Entering Input

After pressing the "Test" button, the Lazy App's CLI interface will appear. If the template requires user input, you will be prompted to provide the necessary information through the CLI. This may include the Shopify store URL, price rule details, and discount code information.

Using the App

After the app is deployed, Lazy will provide you with a dedicated server link to use the API. If you're using FastAPI, you will also receive a link to the API documentation. You can use these links to interact with the app and create price rules and discount codes for your Shopify store.

Integrating the App

If you need to integrate this app into an external service or frontend, you can use the server link provided by Lazy. For example, you might want to add the API endpoints to a custom dashboard or use them in conjunction with other tools that support API requests.

Here's a sample request to create a price rule:

POST /create_price_rule
Host: [Your Lazy Server Link]
Content-Type: application/json

  "shopify_store_url": "",
  "base_data": {
    "title": "10% Off Everything",
    "value": "-10.0",
    "starts_at": "2023-01-01T00:00:00Z"

And here's a sample response you might receive:

  "price_rule_id": "1234567890"

Remember to replace [Your Lazy Server Link] with the actual server link provided by Lazy and with your actual Shopify store URL.

By following these steps, you can quickly set up and use the Shopify Discount Code Creation Template on the Lazy platform to enhance your Shopify store's promotional capabilities.

Last published
June 15, 2024

More templates like this

PDF Data Extraction and Excel Transfer

An app for extracting name, phone number, and email data from PDF files and transferring it to Excel.


GPT-4 Exam Generator

This app uses GPT-4 to generate a PDF of a new exam based on an uploaded PDF of a past exam.


Job Search Website

A customisable one-page job search website with a search bar and a search button that displays UI cards of matching jobs. You need to have a (free) account on to get the API key required for this to work.

React JS
Create Discount Code with Shopify API