Code Examples
This section provides practical code examples for common TakeTheme API operations across multiple programming languages.
Available Languages
Choose your preferred language:
- JavaScript/Node.js — Using
fetch - Python — Using
requests - PHP — Using Guzzle
- Ruby — Using
Net::HTTPand Faraday - Go — Using
net/http
Quick Reference
Fetch All Products
- JavaScript
- Python
- PHP
- Ruby
- Go
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();
import requests
response = requests.get(
'https://api.taketheme.com/api/v1/products',
headers={'tt-api-key': f'{API_KEY}'}
)
data = response.json()['data']
$ch = curl_init('https://api.taketheme.com/api/v1/products');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'tt-api-key: ' . $apiKey,
'Content-Type: application/json'
]
]);
$response = json_decode(curl_exec($ch), true);
$data = $response['data'];
require 'net/http'
require 'json'
uri = URI('https://api.taketheme.com/api/v1/products')
req = Net::HTTP::Get.new(uri)
req['tt-api-key'] = "#{api_key}"
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http|
http.request(req)
}
data = JSON.parse(response.body)['data']
req, _ := http.NewRequest("GET", "https://api.taketheme.com/api/v1/products", nil)
req.Header.Set("tt-api-key", apiKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, _ := client.Do(req)
defer resp.Body.Close()
var result map[string]interface{}
json.NewDecoder(resp.Body).Decode(&result)
data := result["data"]
Create an Order
- JavaScript
- Python
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",
},
}),
});
response = requests.post(
'https://api.taketheme.com/api/v1/orders',
headers={'tt-api-key': f'{API_KEY}'},
json={
'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(`${error.code}: ${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