Update Inventory Quantity with Shopify FastAPI

Test this app for free
369
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')
Get full code

Created: | Last Updated:

Using FastAPI, this app allows users to update inventory quantity of a product and to get inventory_level_id of a product in a Shopify store. 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.

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

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...