by Lazy Sloth
Bulk Update Inventory with Shopify API
import uvicorn
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field
import os
from gql import Client, gql
from gql.transport.requests import RequestsHTTPTransport
app = FastAPI()
# Define the Pydantic model for the bulk inventory update payload
class InventoryAdjustItemInput(BaseModel):
inventory_item_id: str = Field(..., description="The ID of the inventory item to update.")
available_delta: int = Field(..., description="The quantity by which to adjust the inventory level.")
class BulkInventoryUpdateData(BaseModel):
store_url: str = Field(..., description="The URL of the Shopify store.")
location_id: str = Field(..., description="The ID of the location where the inventory is held.")
updates: list[InventoryAdjustItemInput] = Field(..., description="A list of inventory level updates.")
# Define the GraphQL mutation for bulk updating inventory.
BULK_UPDATE_MUTATION = gql("""
mutation bulkUpdateInventory($locationId: ID!, $inventoryItems: [InventoryAdjustItemInput!]!) {
inventoryBulkAdjustQuantityAtLocation(locationId: $locationId, inventoryItemAdjustments: $inventoryItems) {
inventoryLevels {
Created: | Last Updated:
Introduction to the Bulk Update Inventory with Shopify API Template
Welcome to the Bulk Update Inventory with Shopify API template! This template is designed to help you integrate with the Shopify API to manage inventory levels efficiently. With this template, you can quickly set up an application that allows you to bulk update inventory levels and fetch current inventory statuses for products in a Shopify store. This is particularly useful for store owners or developers looking to automate inventory management tasks.
Getting Started
To begin using this template, click on "Start with this Template" in the Lazy platform. This will pre-populate the code in the Lazy Builder interface, so you won't need to copy or paste any code manually.
Initial Setup
Before you can test and use the application, you need to set up an environment secret that the application requires:
SHOPIFY_ADMIN_API_TOKEN
: This is the Shopify admin API token used for authenticating requests to the Shopify GraphQL API.
To obtain your Shopify admin API token, follow these steps:
- Log in to your Shopify admin dashboard.
- Go to "Apps" and then click on "Manage private apps" (if you haven't created one, you will need to create a new private app).
- In the private app settings, ensure that you have the necessary permissions to adjust inventory levels.
- After setting the permissions, you will see the API token generated for your app. Copy this token.
Once you have your Shopify admin API token, go to the Environment Secrets tab within the Lazy Builder and add a new secret with the key SHOPIFY_ADMIN_API_TOKEN
and paste the token you copied as the value.
Test: Pressing the Test Button
After setting up the environment secret, you can press the "Test" button in the Lazy platform. This will begin the deployment of the app and launch the Lazy CLI.
Entering Input
Once the app is deployed, if the code requires user input, the Lazy CLI will prompt you to provide the necessary information. For this template, you will need to input the store URL and location ID when prompted by the CLI.
Using the App
After deployment, Lazy will provide you with a dedicated server link to use the API. Additionally, since this template uses FastAPI, you will also receive a link to the API documentation, which you can use to interact with the endpoints provided by the application:
/bulk_update_inventory
: A POST endpoint that allows you to send a JSON payload with the store URL, location ID, and a list of inventory updates to bulk update inventory levels./fetch_inventory_levels
: A GET endpoint that retrieves the inventory levels for a specific location in a Shopify store, requiring the store URL and location ID as query parameters.
Here is an example of how to make a request to the /bulk_update_inventory
endpoint:
`POST /bulk_update_inventory
Content-Type: application/json
{
"store_url": "your-shopify-store.myshopify.com",
"location_id": "gid://shopify/Location/123456789",
"updates": [
{
"inventory_item_id": "gid://shopify/InventoryItem/987654321",
"available_delta": 10
}
]
}`
And you should expect a response similar to this:
{
"inventoryLevels": [
{
"id": "gid://shopify/InventoryLevel/123456789",
"available": 110
}
]
}
Integrating the App
If you wish to integrate this app with other tools or services, you may need to use the server link provided by Lazy to set up webhooks or API calls from those external tools. Ensure that you have the correct permissions and scopes set up in Shopify to allow for inventory updates.
For example, if you're using a third-party inventory management tool, you might need to add the server link as an endpoint in that tool's settings, allowing it to send inventory update requests to your Lazy app.
By following these steps, you should be able to set up and use the Bulk Update Inventory with Shopify API template on the Lazy platform to manage your Shopify store's inventory levels efficiently.