Skip to main content

Code Examples

This section provides practical code examples for common TakeTheme API operations across multiple programming languages.

Available Languages

Choose your preferred language:

Quick Reference

Fetch All Products

const response = await fetch("https://api.taketheme.com/api/v1/products", {
headers: {
tt-api-key: ` ${API_KEY}`,
"Content-Type": "application/json",
},
});
const { data } = await response.json();

Create an Order

const order = await fetch("https://api.taketheme.com/api/v1/orders", {
method: "POST",
headers: {
tt-api-key: ` ${API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
customer_id: "cust_abc123",
line_items: [{ product_id: "prod_xyz789", quantity: 2 }],
shipping_address: {
name: "John Doe",
line1: "123 Main St",
city: "New York",
state: "NY",
postal_code: "10001",
country: "US",
},
}),
});

Common Patterns

Pagination

async function fetchAllProducts() {
const products = [];
let cursor = null;

do {
const url = new URL("https://api.taketheme.com/api/v1/products");
url.searchParams.set("limit", "100");
if (cursor) url.searchParams.set("cursor", cursor);

const response = await fetch(url, {
headers: { tt-api-key: ` ${API_KEY}` },
});
const { data, pagination } = await response.json();

products.push(...data);
cursor = pagination.next_cursor;
} while (cursor);

return products;
}

Error Handling

try {
const response = await fetch("https://api.taketheme.com/api/v1/products", {
headers: { "tt-api-key": API_KEY },
});

if (!response.ok) {
const error = await response.json();
throw new Error(`${response.status}: ${error.message}`);
}

return await response.json();
} catch (error) {
console.error("API Error:", error.message);
throw error;
}

Rate Limit Handling

async function fetchWithRetry(url, options, maxRetries = 3) {
for (let i = 0; i < maxRetries; i++) {
const response = await fetch(url, options);

if (response.status === 429) {
const retryAfter = response.headers.get("Retry-After") || 1;
await new Promise((r) => setTimeout(r, retryAfter * 1000));
continue;
}

return response;
}

throw new Error("Max retries exceeded");
}

Next Steps

  • Explore language-specific examples in the sidebar
  • Check the API Reference for complete endpoint documentation
  • Learn about webhooks for real-time events