Leveraging Python for Shoplazza API: A Case Study on Order Management
By manhnv, at: 10:53 Ngày 15 tháng 5 năm 2024
Leveraging Python for Shoplazza API: A Case Study on Order Management
Introduction
Welcome to another article in our series, "Leveraging Python for API(s)." In this installment, we will explore how to use Python to interact with the Shoplazza API for order management. This guide covers the basics of making API calls to Shoplazza with Python, demonstrating through a real-world case study how this approach can streamline your order management processes.
Imagine you are a business owner using Shoplazza for your online store. You need to update specific details on many orders regularly. Manually making these updates through Shoplazza's UI is time-consuming and tedious. Python and the Shoplazza API can automate this task, saving you time and reducing errors.
In this guide, you will learn how to set up your environment, authenticate with the Shoplazza API, fetch orders, and update them. We will also cover error handling and best practices for structuring your code.
By the end of this guide, you'll have a solid understanding of how to use Python to interact with the Shoplazza API and apply this knowledge to your own business needs. Let's dive in and start automating your order management!
Shoplazza API Overview
The Shoplazza API is a RESTful web service that uses OAuth 2.0 for authentication and authorization. It allows third-party applications to interact with the Shoplazza platform, enabling various tasks such as querying data, posting transactions, and more.
Shoplazza also provides a method for integration via Private App Access Token, which does not require OAuth 2.0 (this is the method we will use today).
Key Endpoints:
- Orders:
- Endpoint:
/openapi/2020-07/orders
- Methods:
GET
,POST
,PUT
- Description: Allows for the retrieval, creation, and updating of orders within Shoplazza. This is essential for managing your sales and ensuring accurate order records.
- Ref: https://www.shoplazza.dev/v2020.07/reference/order
- Endpoint:
- Products:
- Endpoint:
/openapi/2020-07/products
- Methods:
GET
,POST
,PUT
- Description: Enables access to product information, allowing you to manage your product catalog efficiently.
- Ref: https://www.shoplazza.dev/v2020.07/reference/product
- Endpoint:
- Customers:
- Endpoint:
/openapi/2020-07/customers
- Methods:
GET
,POST
,PUT
- Description: Facilitates the management of customer details, crucial for maintaining up-to-date and accurate customer records.
- Ref: https://www.shoplazza.dev/v2020.07/reference/customer
- Endpoint:
For detailed documentation, please refer to Shoplazza API Documentation.
NOTE: In this blog post, I will demonstrate using the Order endpoint to illustrate the examples. This endpoint is crucial for managing your sales efficiently and provides a comprehensive way to showcase the capabilities of the Shoplazza API.
Configuring the development environment
First, to make the integration/call the Shoplazza API, you will need a store on Shoplazza, if you don’t know how, then you can follow this guide on How To Start an Online Store with Shoplazza
Next, you’ll need to create a Private App on the store that you’ve just created, this will give you the Access Token to access the app via API calls, follow this guide: How To create Shoplazza’s Private App
Finally, set up your development environment with the necessary packages.
-
Install Python: Ensure you have Python installed. You can download it from Python’s official website.
-
Create a virtual environment:
python -m venv venv # create the virtual environment
source venv/bin/activate # active the virtual environment -
Install necessary dependencies:
pip install requests # library for making HTTP(s) request in python
pip install ipython # useful interactive shell for python development
After all the installations are completed, then you can now access the shell by command:
ipython
Authenticating with the Shoplazza API
To make API calls, you'll need to authenticate with Shoplazza using the Access Token from the previous step. To do so, you just need to call the API with a key Access-Token
in the request headers
Also, the url endpoint for calling is https://{your_store_domain}.myshoplaza.com
, which your_store_domain
is the your store domain (For more information, please refer to API Url in Shoplazza).
Here’s a sample code on how to pass the Access Token into the request headers :
NOTE: The below code does not call any API, just for showing that the format of the headers you need to write in order to call the Shoplazza API, so you don’t need to copy or run it.
# Your Shoplazza access token
access_token = "<your access_token="">"
# Shoplazza endpoint you want to call
store_domain = "<your domain="" store="">"
url = f"https://{store_domain}.myshoplaza.com"
# Headers
headers = {
'Access-Token': access_token, # add the Access-Token key into your headers when making requests
'Content-Type': 'application/json' # the content must be in json format
}
# Make the API call
response = requests.get(url, headers=headers)</your></your>
Fetching Orders
Use the Orders API to fetch order(s) from your Shoplazza account.
import requests
# Your Shoplazza access token
access_token = "<your access_token="">"
# Shoplazza endpoint you want to call
store_domain = "<your domain="" store="">"
url = f"https://{store_domain}.myshoplaza.com"
# Orders url
postfix = "/openapi/2020-07/orders"
orders_url = f"{url}{postfix}"
# Headers
headers = {
'Access-Token': access_token,
'Content-Type': 'application/json'
}
# Make the request
response = requests.get(orders_url, headers=headers)
# Check the response status code
if response.status_code == 200:
orders = response.json()
print(orders)
else:
print(f"Error: {response.status_code}")
print(response.text)</your></your>
Run the code above will give you the following result, which contains all the orders in your store:
{
"orders": [
{
"id": "1317632-00000007",
"note": "Total tax amount is: 47.011624",
"number": "00000007",
"financial_status": "paid",
"status": "placed",
"cancel_reason": "",
"payment_method": "markpay",
"fulfillment_status": "waiting",
"discount_code": "",
"customer_note": "",
"tags": "",
"created_at": "2024-04-01T04:42:07Z",
"updated_at": "2024-04-01T04:48:38Z",
"canceled_at": None,
"customer_deleted_at": None,
"deleted_at": None,
"placed_at": "2024-04-01T04:42:07Z",
"browser_ip": "210.245.54.99",
"payment_line": {
"payment_channel": "markpay",
"payment_method": "markpay",
"transaction_no": "",
"merchant_id": "",
"merchant_email": "",
},
"refund_status": "",
"line_items": [
{
"id": "331a7e13-8697-4a62-8acb-fa747a0ae33b",
"product_id": "fd3cba17-73b4-407c-962b-27012217ef57",
"variant_id": "b7156f30-cc6d-48c2-a01b-03661f48b5bf",
"variant_title": "",
"product_title": "Frankenstein",
"product_handle": "frankenstein",
"quantity": 1,
"note": "",
"fulfillment_status": "waiting",
"sku": "FRANK",
"weight_unit": "kg",
"vendor": "",
"product_url": "/products/frankenstein",
"refund_quantity": 0,
"refund_total": "0.00",
"image": "//img.fantaskycdn.com/746e4ea66311e9fc51ea6181d8cf7283.jpeg",
"price": "873.90",
"compare_at_price": "0.00",
"weight": 0,
"total": "873.90",
},
{
"id": "fc38e511-711a-472b-951a-376594a51c6b",
"product_id": "7047cbb4-6146-419f-bc07-64da7d37e40e",
"variant_id": "100e3057-e29d-4cb1-a05f-eafd22b54ddb",
"variant_title": "",
"product_title": "The Great Gatsby",
"product_handle": "the-great-gatsby",
"quantity": 1,
"note": "",
"fulfillment_status": "waiting",
"sku": "TGG",
"weight_unit": "kg",
"vendor": "",
"product_url": "/products/the-great-gatsby",
"refund_quantity": 0,
"refund_total": "0.00",
"image": "//img.fantaskycdn.com/7b77bd990f7a94f30c13a176dde374eb.jpeg",
"price": "123.80",
"compare_at_price": "0.00",
"weight": 0,
"total": "123.80",
},
],
"fulfillments": [],
"landing_site": "",
"buyer_accepts_marketing": False,
"currency": "USD",
"total_price": "997.70",
"total_discount": "0.00",
"total_tax": "0.00",
"total_shipping": "0.00",
"sub_total": "997.70",
"code_discount_total": "0.00",
"line_item_discount_total": "0.00",
"total_refund_price": "0.00",
"total_refund_tax": "0.00",
"total_refund_discount": "0.00",
"shipping_line": {"name": "Sample rate"},
"customer": {
"email": "[email protected]",
"created_at": "2024-04-01T04:19:53Z",
"updated_at": "2024-04-01T04:42:07Z",
"first_name": "Jacques",
"last_name": "Lacroix",
"phone": "+10505281445",
"orders_count": 2,
"total_spent": "1995.40",
},
"shipping_address": {
"first_name": "Jacques",
"last_name": "Lacroix",
"phone": "+18085270019",
"city": "Honolulu",
"zip": "96813",
"province": "Hawaii",
"country": "United States",
"company": "",
"latitude": "0.000000",
"longitude": "0.000000",
"country_code": "US",
"province_code": "US-HI",
"email": "[email protected]",
"phone_area_code": "",
"area": "",
"address1": "3875 Arron Smith Drive",
"address2": "",
"name": "Jacques Lacroix",
},
"billing_address": {
"first_name": "Jacques",
"last_name": "Lacroix",
"city": "Honolulu",
"zip": "96813",
"province": "Hawaii",
"country": "United States",
"company": "",
"latitude": 0,
"longitude": 0,
"country_code": "US",
"province_code": "US-HI",
"email": "[email protected]",
"area": "",
"address1": "3875 Arron Smith Drive",
"address2": "",
"name": "Jacques Lacroix",
},
},
]
}
Updating Orders
To update an order, you need to prepare the order data with the updated details and use the PUT
method to submit the changes.
To update a specific order, you’ll need that Order ID, and the value of the fields that you want to update to that order. For the list of updatable fields, please refer to Order Update API.
In this example, I want to update the note of the Order, and here is how I do it with python:
import requests
# Your Shoplazza access token
access_token = ""
# Shoplazza endpoint you want to call
store_domain = ""
url = f"https://{store_domain}.myshoplaza.com"
# Orders url
order_id = ""
postfix = f"/openapi/2020-07/orders/{order_id}"
orders_url = f"{url}{postfix}"
# Headers
headers = {
'Access-Token': access_token,
'Content-Type': 'application/json'
}
# Data
data = {"note": "The note content of this order is updated via API with python"}
# Make the request
response = requests.put(orders_url, headers=headers, json=data)
# Check the response status code
if response.status_code == 200:
order = response.json()
print(order)
else:
print(f"Error: {response.status_code}")
print(response.text)
After the following code is run, then you can see in your terminal, that your order has been updated with the new note content:
{
"order": {
"id": "1317632-00000007",
"note": "The note content of this order is updated via API with python",
"number": "00000010",
"financial_status": "paid",
"status": "placed",
"cancel_reason": "",
"payment_method": "markpay",
"fulfillment_status": "waiting",
"discount_code": "",
"discount_applications": "[]",
"customer_note": "",
"sales_platform": "shoplazza",
"landing_site": "",
"buyer_accepts_marketing": false,
"currency": "USD",
"total_price": "997.70",
"total_discount": "0.00",
"total_tax": "0.00",
"total_shipping": "0.00",
"sub_total": "997.70",
"code_discount_total": "0.00",
"line_item_discount_total": "0.00",
"tags": "",
"created_at": "2024-07-26T03:44:31Z",
"updated_at": "2024-07-26T03:44:35Z",
"canceled_at": null,
"customer_deleted_at": null,
"deleted_at": null,
"placed_at": "2024-07-26T03:44:31Z",
"total_refund_price": "0.00",
"total_refund_tax": "0.00",
"total_refund_discount": "0.00",
"refund_status": null,
"total_tip_received": "0.00",
"browser_ip": "210.245.54.99",
"source": "",
"last_landing_url": "",
"email_status": "",
"recovery_status": "waiting",
"total_paid": "997.70",
"source_name": {
"data": "",
"created_at": ""
},
"last_referrer_show": {
"data": "",
"created_at": ""
},
"shipping_line": {
"name": "Sample rate"
},
"customer": {
"id": "038b5b70-256d-45cb-804e-8e741cd133bc",
"email": "[email protected]",
"first_name": "Willing ",
"last_name": "Lizard",
"phone": "+18085270019",
"created_at": "2024-01-17T04:25:40Z",
"updated_at": "2024-01-17T12:34:30Z",
"orders_count": 10,
"total_spent": "13420.29"
},
"shipping_address": {
"first_name": "Willing",
"last_name": "Lizard",
"address1": "Smith Drive, Coolbaugh Township, PA, USA",
"address2": "",
"phone": "+18085270019",
"city": "Tobyhanna",
"zip": "18466",
"province": "Pennsylvania",
"country": "United States",
"company": "",
"latitude": "0.000000",
"longitude": "0.000000",
"name": "Willing Lizard",
"country_code": "US",
"province_code": "US-PA",
"phone_area_code": "",
"email": "[email protected]",
"area": "",
"extra_info": null
},
"billing_address": {
"first_name": "Willing",
"last_name": "Lizard",
"address1": "Smith Drive, Coolbaugh Township, PA, USA",
"address2": "",
"city": "Tobyhanna",
"zip": "18466",
"province": "Pennsylvania",
"country": "United States",
"company": "",
"latitude": "0",
"longitude": "0",
"name": "Willing Lizard",
"country_code": "US",
"province_code": "US-PA",
"email": "[email protected]",
"area": "",
"phone": ""
},
"payment_line": {
"payment_name": "Mark payment manually",
"payment_channel": "markpay",
"payment_method": "markpay",
"transaction_no": "",
"merchant_id": "",
"merchant_email": ""
},
"payment_lines": [
{
"payment_name": "Mark payment manually",
"payment_channel": "markpay",
"payment_method": "markpay",
"transaction_no": "",
"merchant_id": "",
"merchant_email": "",
"paid_total": "997.70"
}
],
"line_items": [
{
"id": "a0bea25b-fcaf-4b5a-8559-9202b7b5a7c4",
"product_id": "fd3cba17-73b4-407c-962b-27012217ef57",
"variant_id": "b7156f30-cc6d-48c2-a01b-03661f48b5bf",
"variant_title": "",
"product_title": "Frankenstein",
"product_handle": "frankenstein",
"quantity": 1,
"note": "",
"fulfillment_status": "waiting",
"sku": "FRANK",
"weight_unit": "kg",
"vendor": "",
"product_url": "/products/frankenstein",
"compare_at_price": "0.00",
"image": "//img.fantaskycdn.com/746e4ea66311e9fc51ea6181d8cf7283.jpeg",
"price": "873.90",
"total": "873.90",
"weight": "0.00",
"properties": [],
"custom_properties": null,
"refund_quantity": 0,
"refund_total": "0.00",
"base_price": "0.00",
"discount_applications": "[]",
"requires_shipping": true
},
{
"id": "3ad9e43a-8270-41c1-8c40-8e22b3ee4cd6",
"product_id": "7047cbb4-6146-419f-bc07-64da7d37e40e",
"variant_id": "100e3057-e29d-4cb1-a05f-eafd22b54ddb",
"variant_title": "",
"product_title": "The Great Gatsby",
"product_handle": "the-great-gatsby",
"quantity": 1,
"note": "",
"fulfillment_status": "waiting",
"sku": "TGG",
"weight_unit": "kg",
"vendor": "",
"product_url": "/products/the-great-gatsby",
"compare_at_price": "0.00",
"image": "//img.fantaskycdn.com/7b77bd990f7a94f30c13a176dde374eb.jpeg",
"price": "123.80",
"total": "123.80",
"weight": "0.00",
"properties": [],
"custom_properties": null,
"refund_quantity": 0,
"refund_total": "0.00",
"base_price": "0.00",
"discount_applications": "[]",
"requires_shipping": true
}
],
"fulfillments": [],
"gift_card_total": "0.00",
"shipping_tax_total": "0.00",
"product_tax_included": false,
"refer_info": "{\"default_payment\":\"bogus_gateway\",\"inventory_deduction\":\"order_created\",\"ip\":\"210.245.54.99\",\"source\":\"back\",\"user_agent\":\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36\"}",
"additional_total": "0.00",
"additional_prices": [],
"logistics_code": "",
"config": {
"page_type": "three_step",
"requires_shipping": false,
"checkout_business_type": 0,
"product_tax_included": false,
"checkout_template_type": 0,
"market_setting": null
},
"location_line": null,
"checkout_url": "/checkout/1317632-00000007?key=f97513ed47fcefc3b87fe1df1f1652"
}
}
Conclusion
Congratulations! You've now learned how to make API calls to Shoplazza using Python, streamlining your order management process. By leveraging the Shoplazza API, you can automate routine tasks, saving time and reducing errors, ultimately enhancing the efficiency of your business operations.