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

About this template

A FastAPI application that retrieves all products from a Shopify store and returns them in JSON format. Requires the SHOPIFY_ADMIN_API_TOKEN environment secret. The app includes an endpoint at "/product_metafields" where users can provide a product ID and the Shopify store URL to retrieve the product's metafields. The only environment secret required is SHOPIFY_ADMIN_API_TOKEN, which must be set for the app to authenticate with the Shopify API.

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:

  1. Log in to your Shopify admin dashboard.
  2. Go to "Apps" and then click on "Manage private apps" at the bottom of the page.
  3. 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.
  4. Fill in the necessary details for the private app and ensure that you have the required permissions to access product information.
  5. Once the app is created, you will be provided with an API token. Copy this token.
  6. Back in the Lazy Builder, navigate to the "Environment Secrets" tab.
  7. 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": ""

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!

Last published
April 7, 2024

More templates like this

Customisable Server Status Dashboard

This template allows you to create a dashboard to report about incidents regarding your server to your users.


A bot that answers questions about data

Ask questions about your database via a chat bot. This chatbot connects to a SQLITE database, generates queries for it based on the schema and then runs the queries printing the response all in a nicely styled chat UI. This template is pre-loaded with sample data (car parts) to play around with - here are the sample data columns that you can query: part_number, part_name, price, units_left_in_stock, manufacturer .


WhatsApp Bot Builder

The WhatsApp Bot Builder app allows users to create a WhatsApp bot that responds to every message with "Hello World".

Get Product Metafields using Shopify API