Update Inventory Quantity with Shopify API

 import uvicorn
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import requests
import os
import json

app = FastAPI()

class InventoryUpdateData(BaseModel):
    store_url: str
    inventory_item_id: int
    available_adjustment: int
    location_id: int

class InventoryQueryData(BaseModel):
    store_url: str
    product_id: int

@app.post("/update_inventory", summary="Update Inventory", description="Endpoint to adjust the inventory level of a product.")
async def update_inventory(data: InventoryUpdateData):
    # Get the Shopify admin API token from environment variables.
    shopify_admin_api_token = os.environ.get('SHOPIFY_ADMIN_API_TOKEN')

About this template

This app allows users to update inventory quantity of a product and to get inventory_level_id of a product in a Shopify store using API. Users can set the necessary environment variables, including the SHOPIFY_ADMIN_API_TOKEN to enable the app to access the store. The app has following end points: - An endpoint to adjust the inventory level of a product (`/update_inventory`). - An endpoint to get the inventory level ID of a product (`/get_inventory_level_id`).

Introduction to the Inventory Update Template with Shopify API

Welcome to the Inventory Update Template with Shopify API! This template is designed to help you seamlessly update inventory quantities and retrieve inventory level IDs for products in a Shopify store. Whether you're a store owner, developer, or a non-technical builder, this guide will walk you through the process of using this template on the Lazy platform.

Getting Started

To begin using this template, simply click on "Start with this Template" on the Lazy platform. 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

Before you can use the template, you'll need to set up an environment secret within the Lazy Builder. This secret is the Shopify admin API token, which allows the app to authenticate with your Shopify store.

  • Go to the Environment Secrets tab within the Lazy Builder.
  • Create a new secret with the key SHOPIFY_ADMIN_API_TOKEN.
  • To obtain your Shopify admin API token, you'll need to create a private app in your Shopify admin. Follow the instructions provided by Shopify here to generate the credentials.
  • Once you have your token, enter it as the value for the SHOPIFY_ADMIN_API_TOKEN secret.


With the environment secret set, you're ready to test the app. Press the "Test" button on the Lazy platform. This will deploy the app and launch the Lazy CLI.

Entering Input

After pressing the "Test" button, the Lazy App's CLI interface will appear. If the app requires any user input, you will be prompted to provide it there. For this template, you will need to input details such as the store URL, inventory item ID, available adjustment, location ID, and product ID when prompted by the CLI.

Using the App

Once the app is running, you can interact with it using the provided API endpoints:

  • To update inventory, send a POST request to the /update_inventory endpoint with the necessary data.
  • To get the inventory level ID, send a POST request to the /get_inventory_level_id endpoint with the required product information.

Lazy will provide you with a dedicated server link to use the API. Additionally, since this template uses FastAPI, you will also be provided with a docs link where you can interact with the API and see the available endpoints and their specifications.

Integrating the App

If you need to integrate this app with an external service or frontend, you can use the server link provided by Lazy. For example, you might want to call the API from a custom frontend application or integrate it with other tools that your Shopify store uses.

Here's a sample request to update inventory:

POST /update_inventory HTTP/1.1
Host: [Your Lazy Server Link]
Content-Type: application/json

  "store_url": "yourstore.myshopify.com",
  "inventory_item_id": 1234567890,
  "available_adjustment": 5,
  "location_id": 1234567890

And a sample response might look like this:

  "message": "Inventory adjusted successfully."

Follow these steps to ensure your app is set up correctly and ready to manage inventory on your Shopify store. If you encounter any issues or have questions, the Lazy platform's customer support is available to assist you.

Last published
April 7, 2024

More templates like this

Customisable Server Status Dashboard

This template allows you to create a dashboard to report about incidents regarding your server to your users.


A bot that answers questions about data

Ask questions about your database via a chat bot. This chatbot connects to a SQLITE database, generates queries for it based on the schema and then runs the queries printing the response all in a nicely styled chat UI. This template is pre-loaded with sample data (car parts) to play around with - here are the sample data columns that you can query: part_number, part_name, price, units_left_in_stock, manufacturer .


WhatsApp Bot Builder

The WhatsApp Bot Builder app allows users to create a WhatsApp bot that responds to every message with "Hello World".

Update Inventory Quantity with Shopify API