by Lazy Sloth
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')
Update Inventory Quantity with Shopify API
Created: | Last Updated:
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.
Test
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.
Technologies
![Fast API](https://gitlab.com/uploads/-/system/project/avatar/37467835/fastapi.png)
![Shopify](https://cdn.iconscout.com/icon/premium/png-256-thumb/shopify-2749341-2284731.png?f=webp&w=128)
![Python](https://i.pinimg.com/originals/2f/9c/11/2f9c11f9e55efbf1791f12c06d60729b.jpg)