by Lazy Sloth
Get Product Metafields using Shopify API
import uvicorn
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import requests
import os
app = FastAPI()
class ProductMetafieldData(BaseModel):
product_id: str
shopify_store_url: str
# Endpoint to get product metafields by product ID.
@app.post("/product_metafields", summary="Get Product Metafields", description="Endpoint to get product metafields by product ID.")
async def get_product_metafields(data: ProductMetafieldData):
# Get the Shopify admin API token from environment variables.
api_token = os.environ.get('SHOPIFY_ADMIN_API_TOKEN')
# Check if the admin API token is set.
if not api_token:
raise HTTPException(status_code=400, detail="Missing Shopify admin API token. Please set SHOPIFY_ADMIN_API_TOKEN environment variable.")
# Check if the product ID and store URL are set.
Created: | Last Updated:
Introduction to the Shopify Product Metafields Retrieval Template
Welcome to the Shopify Product Metafields Retrieval Template! This template is designed to help you quickly set up an application that retrieves metafields for products from a Shopify store using the Shopify API. It's perfect for builders who want to integrate Shopify product information into their own applications or services without worrying about the complexities of API integration and server deployment.
Getting Started with the Template
To begin using this template, simply 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, paste, or delete any code manually.
Initial Setup: Adding Environment Secrets
Before you can use this template, you'll need to set up an environment secret within the Lazy Builder. The code requires a Shopify admin API token to authenticate with the Shopify API. Here's how to acquire and set up your Shopify admin API token:
- Log in to your Shopify admin dashboard.
- Go to "Apps" and then click on "Manage private apps" at the bottom of the page.
- If you haven't created a private app yet, click on "Create a new private app". If you have an existing app, you can use its API token.
- Fill in the necessary details for the private app and ensure that you have the required permissions to access product information.
- Once the app is created, you will be provided with an API token. Copy this token.
- Back in the Lazy Builder, navigate to the "Environment Secrets" tab.
- Create a new secret with the key `SHOPIFY_ADMIN_API_TOKEN` and paste the API token you copied as the value.
With the environment secret set, you're ready to deploy the application.
Test: Deploying the App
Press the "Test" button to begin the deployment of your app. The Lazy platform will handle all the necessary server setup and deployment processes for you.
Entering Input: Providing Product and Store Information
After pressing the "Test" button, the Lazy App's CLI interface will appear. You will be prompted to provide the following input:
product_id
: The unique identifier for the product in your Shopify store.shopify_store_url
: The URL of your Shopify store.
These inputs are necessary for the app to retrieve the metafields for the specified product.
Using the App: Interacting with the API
Once the app is deployed, you will receive a dedicated server link to interact with the API. Additionally, since this template uses FastAPI, you will also be provided with a link to the API documentation. This documentation will allow you to explore the available endpoints and their expected parameters.
To retrieve the metafields for a product, you will use the provided server link to send a POST request to the "/product_metafields" endpoint with the required JSON body containing the `product_id` and `shopify_store_url`.
Here's a sample request you might send:
`POST /product_metafields HTTP/1.1
Host: your-server-link
Content-Type: application/json
{
"product_id": "1234567890",
"shopify_store_url": "yourstore.myshopify.com"
}`
And a sample response you might receive:
[
{
"id": 1010101010,
"namespace": "inventory",
"key": "warehouse",
"value": "42",
"value_type": "integer",
"description": "Warehouse number with inventory"
},
// ... more metafields
]
Integrating the App: Next Steps
After successfully retrieving product metafields, you may want to integrate this data into your own application or service. Depending on your needs, this could involve:
- Storing the metafields in your own database for further processing.
- Displaying the metafields in a user interface for your customers or staff.
- Using the metafields to enhance product listings on your platform.
If you need to integrate the API into an external tool, you will use the server link provided by Lazy. For example, you might add the API endpoint to a web application to fetch product details dynamically.
Remember, the Lazy platform has taken care of the deployment, so you can focus on how to use the API in your project. Happy building!