by minahil
Get Orders using Shopify API
import uvicorn
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import requests
import os
app = FastAPI()
# Environment variable for the Shopify store URL
SHOPIFY_STORE_URL = os.environ['SHOPIFY_STORE_URL']
class Order(BaseModel):
pass
@app.get("/orders", summary="Get Orders", description="Retrieves orders from a Shopify store.")
async def get_orders():
orders_url = f"https://{SHOPIFY_STORE_URL}/admin/api/2024-01/orders.json"
headers = {
'Content-Type': 'application/json',
'X-Shopify-Access-Token': os.environ['SHOPIFY_ADMIN_API_TOKEN']
}
response = requests.get(orders_url, headers=headers)
if response.status_code == 200:
return response.json()
Get Orders using Shopify API
Created: | Last Updated:
Introduction to the Shopify API Get Orders Template
This template helps you create a Python application using FastAPI to retrieve orders from a Shopify store. The app requires a SHOPIFY_ADMIN_API_TOKEN
to authenticate requests.
Clicking Start with this Template
To get started with this template, click the "Start with this Template" button.
Initial Setup
This template requires setting up environment secrets. Follow these steps to configure the necessary environment secrets:
- SHOPIFY_STORE_URL: This is the URL of your Shopify store. You can find this in your Shopify admin dashboard.
- SHOPIFY_ADMIN_API_TOKEN: This token is required to authenticate API requests. Follow these steps to get your API token:
- Log in to your Shopify admin dashboard.
- Go to Apps.
- Click on Manage private apps.
- Create a new private app or use an existing one.
- Under Admin API, ensure you have the necessary permissions to read orders.
- Copy the Admin API access token.
In the Lazy Builder, navigate to the Environment Secrets tab and add the following secrets:
- SHOPIFY_STORE_URL
- SHOPIFY_ADMIN_API_TOKEN
Test
Press the "Test" button to deploy the app. The Lazy CLI will handle the deployment process.
Using the App
Once the app is deployed, you will be provided with a server link through the Lazy builder CLI. You can use this link to interact with the API. Additionally, since the app uses FastAPI, you will also receive a link to the automatically generated API documentation.
Integrating the App
To retrieve orders from your Shopify store, you can use the provided API endpoint. Below is a sample request and response:
Sample Request
GET /orders
Host: [Your Lazy App Server Link]
Content-Type: application/json
X-Shopify-Access-Token: [Your SHOPIFY_ADMIN_API_TOKEN]
Sample Response
{
"orders": [
{
"id": 123456789,
"email": "customer@example.com",
"created_at": "2023-10-01T12:34:56-04:00",
"total_price": "100.00",
"currency": "USD",
"line_items": [
{
"id": 987654321,
"title": "Sample Product",
"quantity": 1,
"price": "100.00"
}
]
}
]
}
You can integrate this API endpoint into your existing systems or tools to automate order retrieval from your Shopify store.
Technologies
![AWS](https://cdn.iconscout.com/icon/premium/png-256-thumb/aws-2749233-2284623.png?f=webp&w=128)
![Fast API](https://gitlab.com/uploads/-/system/project/avatar/37467835/fastapi.png)
![Shopify](https://cdn.iconscout.com/icon/premium/png-256-thumb/shopify-2749341-2284731.png?f=webp&w=128)
![Python](https://i.pinimg.com/originals/2f/9c/11/2f9c11f9e55efbf1791f12c06d60729b.jpg)