Python Examples
Python examples using the requests library.
Setup
pip install requests
import os
import requests
TV_API_KEY = os.environ["TV_API_KEY"] # tv_key_...
TV_BASE = os.environ["TV_BASE_URL"] # https://api.acme.trackvision.ai/v1
def auth_headers():
return {"Authorization": f"Bearer {TV_API_KEY}"}
List Products
def list_products(page=1, per_page=50):
response = requests.get(
f"{TV_BASE}/products",
headers=auth_headers(),
params={"page": page, "per_page": per_page},
)
response.raise_for_status()
return response.json()
data = list_products()
print(f"Found {data['pagination']['total']} products")
for product in data["products"]:
print(f" {product['sku']}: {product['name']}")
Get a Product
def get_product(product_id):
response = requests.get(
f"{TV_BASE}/products/{product_id}",
headers=auth_headers(),
)
response.raise_for_status()
return response.json()
product = get_product("a1b2c3d4-e5f6-7890-abcd-ef1234567890")
print(product["name"])
Create a Product
def create_product(data):
response = requests.post(
f"{TV_BASE}/products",
headers=auth_headers(),
json=data,
)
response.raise_for_status()
return response.json()
new_product = create_product({
"name": "Organic Cotton T-Shirt",
"sku": "TSHIRT-001",
"gtin": "09506000134352",
"category": "apparel",
"color": "White",
"size": "M",
"weight": 0.2,
"weightUnit": "kg",
})
print(f"Created product with ID: {new_product['id']}")
Update a Product
def update_product(product_id, data):
response = requests.put(
f"{TV_BASE}/products/{product_id}",
headers=auth_headers(),
json=data,
)
response.raise_for_status()
return response.json()
updated = update_product(
"a1b2c3d4-e5f6-7890-abcd-ef1234567890",
{"variant": "White / L", "size": "L"},
)
print(f"Updated: {updated['name']}")
Fetch DPP Data
def get_product_dpp(product_id):
response = requests.get(
f"{TV_BASE}/products/{product_id}/dpp",
headers=auth_headers(),
)
response.raise_for_status()
return response.json()
dpp = get_product_dpp("a1b2c3d4-e5f6-7890-abcd-ef1234567890")
subject = dpp["credentialSubject"]
print("Issuer:", dpp["issuer"])
if "composition" in subject:
for material in subject["composition"].get("materials", []):
print(f" {material['name']}: {material['percentage']}%")
if "environmental" in subject:
env = subject["environmental"]
print(f"Carbon footprint: {env.get('carbonFootprintTotal')} kgCO2e")
Get Supplier Certifications
def get_supplier_certifications(supplier_id):
response = requests.get(
f"{TV_BASE}/suppliers/{supplier_id}/certifications",
headers=auth_headers(),
)
response.raise_for_status()
return response.json()
certs = get_supplier_certifications("b2c3d4e5-f6a7-8901-bcde-f12345678901")
for cert in certs.get("certifications", []):
print(f" {cert['name']} — expires {cert.get('expiryDate', 'N/A')}")
Download Site ZIP
def download_site(site_id, output_path):
response = requests.get(
f"{TV_BASE}/sites/{site_id}/download",
headers=auth_headers(),
stream=True,
)
response.raise_for_status()
with open(output_path, "wb") as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
print(f"Site saved to {output_path}")
download_site("c3d4e5f6-a7b8-9012-cdef-012345678902", "my-site.zip")
Paginate Through All Products
def get_all_products():
all_products = []
page = 1
while True:
data = list_products(page=page, per_page=200)
all_products.extend(data["products"])
pagination = data["pagination"]
if page >= pagination["totalPages"]:
break
page += 1
return all_products
products = get_all_products()
print(f"Fetched all {len(products)} products")
Read DPP from GS1 Resolver (Public)
def get_dpp_from_resolver(gtin, domain="acme.trackvision.ai"):
response = requests.get(
f"https://resolver.{domain}/01/{gtin}",
params={"linkType": "gs1:dpp"},
headers={"Accept": "application/ld+json"},
)
response.raise_for_status()
return response.json()
dpp = get_dpp_from_resolver("09506000134352")
print("Issuer:", dpp["issuer"])
print("Issued:", dpp["issuanceDate"])