JavaScript Examples
JavaScript examples using the native fetch API (works in Node.js 18+ and all modern browsers).
Setup
const TV_API_KEY = process.env.TV_API_KEY; // tv_key_...
const TV_BASE = process.env.TV_BASE_URL; // https://api.acme.trackvision.ai/v1
function authHeader() {
return { Authorization: `Bearer ${TV_API_KEY}` };
}
List Products
async function listProducts(page = 1, perPage = 50) {
const params = new URLSearchParams({ page, per_page: perPage });
const res = await fetch(`${TV_BASE}/products?${params}`, {
headers: authHeader(),
});
if (!res.ok) {
const err = await res.json();
throw new Error(`${res.status}: ${err.error.message}`);
}
return res.json(); // { products: [...], pagination: { ... } }
}
const { products, pagination } = await listProducts();
console.log(`Found ${pagination.total} products`);
Get a Product
async function getProduct(id) {
const res = await fetch(`${TV_BASE}/products/${id}`, {
headers: authHeader(),
});
if (!res.ok) {
const err = await res.json();
throw new Error(`${res.status}: ${err.error.message}`);
}
return res.json();
}
const product = await getProduct("a1b2c3d4-e5f6-7890-abcd-ef1234567890");
console.log(product.name);
Create a Product
async function createProduct(data) {
const res = await fetch(`${TV_BASE}/products`, {
method: "POST",
headers: {
...authHeader(),
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
if (!res.ok) {
const err = await res.json();
throw new Error(`${res.status}: ${err.error.message}`);
}
return res.json();
}
const newProduct = await createProduct({
name: "Organic Cotton T-Shirt",
sku: "TSHIRT-001",
gtin: "09506000134352",
category: "apparel",
color: "White",
size: "M",
weight: 0.2,
weightUnit: "kg",
});
console.log("Created:", newProduct.id);
Fetch DPP Data
async function getProductDpp(productId) {
const res = await fetch(`${TV_BASE}/products/${productId}/dpp`, {
headers: authHeader(),
});
if (!res.ok) {
const err = await res.json();
throw new Error(`${res.status}: ${err.error.message}`);
}
return res.json(); // W3C Verifiable Credential JSON-LD
}
const dpp = await getProductDpp("a1b2c3d4-e5f6-7890-abcd-ef1234567890");
const { composition, environmental } = dpp.credentialSubject;
console.log("Materials:", composition.materials);
console.log("Carbon footprint:", environmental.carbonFootprintTotal, "kgCO2e");
Download a Site ZIP
import { writeFile } from "fs/promises";
async function downloadSite(siteId, outputPath) {
const res = await fetch(`${TV_BASE}/sites/${siteId}/download`, {
headers: authHeader(),
});
if (!res.ok) {
const err = await res.json();
throw new Error(`${res.status}: ${err.error.message}`);
}
const buffer = await res.arrayBuffer();
await writeFile(outputPath, Buffer.from(buffer));
console.log(`Site saved to ${outputPath}`);
}
await downloadSite("c3d4e5f6-a7b8-9012-cdef-012345678902", "./my-site.zip");
Paginate Through All Products
async function getAllProducts() {
const allProducts = [];
let page = 1;
let totalPages = 1;
while (page <= totalPages) {
const { products, pagination } = await listProducts(page, 200);
allProducts.push(...products);
totalPages = pagination.totalPages;
page++;
}
return allProducts;
}
const products = await getAllProducts();
console.log(`Fetched all ${products.length} products`);
Read DPP from GS1 Resolver (Public)
async function getDppFromResolver(gtin) {
const res = await fetch(
`https://resolver.acme.trackvision.ai/01/${gtin}?linkType=gs1:dpp`,
{ headers: { Accept: "application/ld+json" } }
);
if (!res.ok) throw new Error(`Resolver returned ${res.status}`);
return res.json();
}
const dpp = await getDppFromResolver("09506000134352");
console.log("Issuer:", dpp.issuer);