Skip to main content
How-to

Python SDK

Install the Python SDK, build an authenticated client, and retrieve an order.

Before you start

  • A Simple Key. Generate one under Configuration → Back Office → Authorized Applications. See Creating a Simple Key. For an application that multiple merchants connect to their own accounts, use OAuth 2.0 instead.
  • Python 3.6 or newer.

Install

pip install ultracart-rest-sdk

The package installs under the name ultracart-rest-sdk and imports as ultracart:

import ultracart

Authenticate

The Python SDK has no one-line convenience constructor. Build a Configuration, set the key on it, then pass the API version header to ApiClient:

import ultracart
from ultracart import ApiClient
from ultracart.apis import OrderApi


def api_client():
config = ultracart.Configuration()
config.api_key['x-ultracart-simple-key'] = 'YOUR_API_KEY' # <- your merchant Simple Key
return ApiClient(
configuration=config,
header_name='X-UltraCart-Api-Version',
header_value='2017-03-01',
)


order_api = OrderApi(api_client())

Both halves are required. The key authenticates the request and the header selects the API version, and a request missing either one fails. See Versioning for what the version controls.

warning

Read the key from the environment or a secret store rather than hardcoding it. The samples repository hardcodes a shared development key and sets verify_ssl = False; neither belongs in your code. Leave TLS verification on.

Retrieve an order

This is the get_order sample from sdk_samples, trimmed to the call itself:

# Trimmed from sdk_samples/python/order/get_order.py

expand = "item,summary,billing,shipping,shipping.tracking_number_details"
order_id = 'DEMO-0009104390' # <- an order ID in your account

api_response = order_api.get_order(order_id, expand=expand)

if hasattr(api_response, 'error') and api_response.error:
print(f"Developer Message: {api_response.error.developer_message}")
print(f"User Message: {api_response.error.user_message}")
exit()

print(api_response.order)

The expand argument controls how much of the order comes back. Order objects are large, and requesting every branch across thousands of orders is the most common cause of slow SDK code. Expanding objects lists the valid values.

UltraCart application errors arrive on api_response.error with a developer_message and a user_message, rather than as a raised exception.

Next

Was this page helpful?