View as MarkdownBest Practices & Examples
1. Always Handle Errors#
async function getBrands() {
try {
const response = await fetch('https://your-store.omnicart.cc/admin/brands', {
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
}
})
if (!response.ok) {
const error = await response.json()
throw new Error(error.message || 'Request failed')
}
return await response.json()
} catch (error) {
console.error('Failed to fetch brands:', error)
throw error
}
}
const brands = await fetch('/admin/brands?limit=10000')
async function getAllBrands() {
let allBrands = []
let offset = 0
const limit = 100
while (true) {
const { brands, count } = await fetch(
`/admin/brands?offset=${offset}&limit=${limit}`
)
allBrands.push(...brands)
if (offset + limit >= count) break
offset += limit
}
return allBrands
}
3. Cache Tokens Appropriately#
localStorage.setItem('admin_token', token)
const token = localStorage.getItem('admin_token')
localStorage.removeItem('admin_token')
function createBrand(data) {
if (!data.name || data.name.length < 2) {
throw new Error('Brand name must be at least 2 characters')
}
if (data.website && !isValidUrl(data.website)) {
throw new Error('Invalid website URL')
}
return fetch('/admin/brands', {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
}
5. Use Filtering to Reduce Response Size#
const brands = await fetch('/admin/brands')
const activeBrands = await fetch('/admin/brands?status=active')
const brandNames = await fetch('/admin/brands?status=active&fields=id,name,slug')
6. Implement Retry Logic for Network Errors#
async function fetchWithRetry(url, options, retries = 3) {
for (let i = 0; i < retries; i++) {
try {
const response = await fetch(url, options)
return response
} catch (error) {
if (i === retries - 1) throw error
await new Promise(resolve => setTimeout(resolve, 1000 * (i + 1)))
}
}
}
7. Set Appropriate Timeouts#
const controller = new AbortController()
const timeoutId = setTimeout(() => controller.abort(), 10000)
try {
const response = await fetch('/admin/brands', {
signal: controller.signal,
headers: { 'Authorization': `Bearer ${token}` }
})
return await response.json()
} finally {
clearTimeout(timeoutId)
}
Complete Examples#
Example 1: Create and Retrieve a Brand#
curl -X POST 'https://your-store.omnicart.cc/admin/brands' \
-H 'Authorization: Bearer <admin_token>' \
-H 'Content-Type: application/json' \
-d '{
"name": "Nike",
"description": "Just Do It - Athletic apparel and footwear",
"website": "https://example-brand.com",
"status": "active",
"metadata": {
"founded": 1964,
"headquarters": "Oregon, USA"
}
}'
{
"brand": {
"id": "brand_01HQZX...",
"name": "Nike",
"slug": "nike",
"description": "Just Do It - Athletic apparel and footwear",
"website": "https://example-brand.com",
"status": "active",
"metadata": {
"founded": 1964,
"headquarters": "Oregon, USA"
},
"created_at": "2026-01-12T10:30:00Z",
"updated_at": "2026-01-12T10:30:00Z"
}
}
curl -X GET 'https://your-store.omnicart.cc/admin/brands/brand_01HQZX...' \
-H 'Authorization: Bearer <admin_token>'
{
"brand": {
"id": "brand_01HQZX...",
"name": "Nike",
"slug": "nike",
"description": "Just Do It - Athletic apparel and footwear",
"website": "https://example-brand.com",
"status": "active",
"created_at": "2026-01-12T10:30:00Z",
"updated_at": "2026-01-12T10:30:00Z"
}
}
Example 2: Partner Authentication Flow#
curl -X POST 'https://your-store.omnicart.cc/auth/partner/emailpass/register' \
-H 'Content-Type: application/json' \
-d '{
"email": "partner@example.com",
"password": "SecurePass123!"
}'
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
curl -X POST 'https://your-store.omnicart.cc/store/partners/auth/register' \
-H 'x-publishable-api-key: pk_YOUR_PUBLISHABLE_KEY' \
-H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...' \
-H 'Content-Type: application/json' \
-d '{
"company_name": "Example Corp",
"contact_name": "John Doe",
"phone": "+1-555-0100",
"website": "https://example.com"
}'
{
"partner": {
"id": "partner_01HQZX...",
"company_name": "Example Corp",
"contact_name": "John Doe",
"email": "partner@example.com",
"status": "pending"
}
}
curl -X POST 'https://your-store.omnicart.cc/auth/partner/emailpass' \
-H 'Content-Type: application/json' \
-d '{
"email": "partner@example.com",
"password": "SecurePass123!"
}'
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
curl -X GET 'https://your-store.omnicart.cc/store/partners/me' \
-H 'x-publishable-api-key: pk_YOUR_PUBLISHABLE_KEY' \
-H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...'
{
"partner": {
"id": "partner_01HQZX...",
"company_name": "Example Corp",
"contact_name": "John Doe",
"email": "partner@example.com",
"phone": "+1-555-0100",
"website": "https://example.com",
"status": "active",
"stats": {
"total_clicks": 1250,
"total_conversions": 43,
"total_revenue": 12750.50,
"total_commission_earned": 1912.58,
"pending_commission": 1912.58,
"conversion_rate": 3.44
}
}
}
Example 3: Paginated Brand Listing with Search#
curl -X GET 'https://your-store.omnicart.cc/admin/brands?q=sport&status=active&offset=0&limit=20' \
-H 'Authorization: Bearer <admin_token>'
{
"brands": [
{
"id": "brand_01HQZX...",
"name": "Nike",
"slug": "nike",
"status": "active"
},
{
"id": "brand_01HQZY...",
"name": "Adidas",
"slug": "adidas",
"status": "active"
},
// ... 18 more brands
],
"count": 87,
"offset": 0,
"limit": 20
}
curl -X GET 'https://your-store.omnicart.cc/admin/brands?q=sport&status=active&offset=20&limit=20' \
-H 'Authorization: Bearer <admin_token>'
Example 4: Error Handling#
curl -X GET 'https://your-store.omnicart.cc/admin/brands'
{
"message": "Authentication required",
"type": "unauthorized"
}
curl -X GET 'https://your-store.omnicart.cc/admin/brands' \
-H 'Authorization: Bearer invalid_token'
{
"message": "Invalid token",
"type": "unauthorized"
}
curl -X GET 'https://your-store.omnicart.cc/admin/brands/brand_invalid' \
-H 'Authorization: Bearer <admin_token>'
{
"message": "Brand not found",
"type": "not_found"
}
curl -X POST 'https://your-store.omnicart.cc/admin/brands' \
-H 'Authorization: Bearer <admin_token>' \
-H 'Content-Type: application/json' \
-d '{
"description": "Missing name field"
}'
{
"message": "Invalid brand data: name is required",
"type": "invalid_data"
}
Example 5: Update Partner Profile#
curl -X PATCH 'https://your-store.omnicart.cc/store/partners/me' \
-H 'x-publishable-api-key: pk_YOUR_PUBLISHABLE_KEY' \
-H 'Authorization: Bearer <partner_token>' \
-H 'Content-Type: application/json' \
-d '{
"contact_name": "Jane Doe",
"phone": "+1-555-0200",
"website": "https://example-brand-updated.com",
"payment_method": "paypal",
"payment_email": "payments@example.com"
}'
{
"partner": {
"id": "partner_01HQZX...",
"company_name": "Example Corp",
"contact_name": "Jane Doe",
"email": "partner@example.com",
"phone": "+1-555-0200",
"website": "https://example-brand-updated.com",
"status": "active",
"payment_method": "paypal",
"payment_email": "payments@example.com"
}
}
Example 6: Public Endpoint (No Auth)#
curl -X GET 'https://your-store.omnicart.cc/flow-builder/session/create?cart_id=cart_123&email=customer@example.com'
{
"session_id": "fb_session_abc123",
"cart_id": "cart_123",
"email": "customer@example.com",
"created_at": "2026-01-12T10:30:00Z"
}
curl -X GET 'https://your-store.omnicart.cc/track/click?code=PARTNER123&product_id=prod_456'