Bulk Update Inventory with Shopify API

Test this app for free
61
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 {
Get full code

Created: | Last Updated:

The app includes two main functionalities: 1. A POST endpoint `/bulk_update_inventory` that allows bulk updating of inventory levels for products in a Shopify store. It requires a JSON payload with the store URL, location ID, and a list of inventory updates. 2. A GET endpoint `/fetch_inventory_levels` that retrieves the inventory levels for a specific location in a Shopify store. It requires the store URL and location ID as query parameters. For the app to function correctly, please ensure the following environment variable is set in the Env Secrets tab: - `SHOPIFY_ADMIN_API_TOKEN`: This is the Shopify admin API token used for authenticating requests to the Shopify GraphQL API.

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:

  1. Log in to your Shopify admin dashboard.
  2. 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).
  3. In the private app settings, ensure that you have the necessary permissions to adjust inventory levels.
  4. 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.

Technologies

Boost Shopify with Lazy AI: Automate Store Management, API Integration, Marketing and More  Boost Shopify with Lazy AI: Automate Store Management, API Integration, Marketing and More
FastAPI Templates and Webhooks FastAPI Templates and Webhooks

Similar templates

FastAPI endpoint for Text Classification using OpenAI GPT 4

This API will classify incoming text items into categories using the Open AI's GPT 4 model. If the model is unsure about the category of a text item, it will respond with an empty string. The categories are parameters that the API endpoint accepts. The GPT 4 model will classify the items on its own with a prompt like this: "Classify the following item {item} into one of these categories {categories}". There is no maximum number of categories a text item can belong to in the multiple categories classification. The API will use the llm_prompt ability to ask the LLM to classify the item and respond with the category. The API will take the LLM's response as is and will not handle situations where the model identifies multiple categories for a text item in the single category classification. If the model is unsure about the category of a text item in the multiple categories classification, it will respond with an empty string for that item. The API will use Python's concurrent.futures module to parallelize the classification of text items. The API will handle timeouts and exceptions by leaving the items unclassified. The API will parse the LLM's response for the multiple categories classification and match it to the list of categories provided in the API parameters. The API will convert the LLM's response and the categories to lowercase before matching them. The API will split the LLM's response on both ':' and ',' to remove the "Category" word from the response. The temperature of the GPT model is set to a minimal value to make the output more deterministic. The API will return all matching categories for a text item in the multiple categories classification. The API will strip any leading or trailing whitespace from the categories in the LLM's response before matching them to the list of categories provided in the API parameters. The API will accept lists as answers from the LLM. If the LLM responds with a string that's formatted like a list, the API will parse it and match it to the list of categories provided in the API parameters.

Icon 1 Icon 1
251

We found some blogs you might like...