Skip to main content

Error Handling

Understanding and handling API errors.

HTTP Status Codes

CodeMeaningAction
200SuccessRequest completed
201CreatedResource created
400Bad RequestCheck request format
401UnauthorizedToken expired or invalid
403ForbiddenInsufficient permissions
404Not FoundResource doesn't exist
422Validation ErrorFix request data
429Rate LimitedSlow down requests
500Server ErrorRetry or contact support

Error Response Format

All errors return a consistent JSON structure:

{
"statusCode": 422,
"error": "Unprocessable Entity",
"message": "Validation failed",
"details": [
{
"field": "consumer.email",
"message": "\"email\" must be a valid email"
}
]
}

Handling Errors in Code

interface ApiError {
statusCode: number;
error: string;
message: string;
details?: Array<{
field: string;
message: string;
}>;
}

async function createOrder(order: OrderPayload): Promise<Order> {
const response = await fetch("https://na1-prod.okcapsule.app/v2/orders", {
method: "POST",
headers: {
Authorization: `Bearer ${access_token}`,
"Content-Type": "application/json",
},
body: JSON.stringify(order),
});

if (!response.ok) {
const error: ApiError = await response.json();

switch (response.status) {
case 401:
// Token expired - refresh and retry
await refreshToken();
return createOrder(order);

case 422:
// Validation error - log details
console.error("Validation errors:", error.details);
throw new Error(`Validation failed: ${error.message}`);

case 429:
// Rate limited - wait and retry
const retryAfter = response.headers.get("Retry-After") || "60";
await sleep(parseInt(retryAfter) * 1000);
return createOrder(order);

default:
throw new Error(`API error: ${error.message}`);
}
}

const data = await response.json();
return data.order;
}

Common Validation Errors

Consumer Errors

ErrorCauseFix
"email" must be a valid emailInvalid email formatCheck email syntax
"last_name" is requiredMissing required fieldInclude last_name with first_name
"phone_number" length must be less than or equal to 15Phone too longUse E.164 format

Address Errors

ErrorCauseFix
"country_name" is requiredMissing countryAdd country_name
"address1" length must be less than or equal to 100Address too longShorten address
Invalid postal code for countryPostal code mismatchVerify postal code format

Order Errors

ErrorCauseFix
"order_lines" must contain at least 1 itemsEmpty orderAdd order_lines with pouches
Invalid client_product_idProduct not foundCheck product exists and is active
Either pack_id or contents requiredEmpty pouchAdd contents or pack_id
Maximum 8 supplements per pouchToo many itemsReduce pouch contents

Product Errors

ErrorCauseFix
Product is not activeUsing inactive productFilter for active=true
Product not foundInvalid product IDVerify product ID

Authentication Errors

401 Unauthorized

{
"statusCode": 401,
"error": "Unauthorized",
"message": "Token expired"
}

Solution: Refresh your access token:

async function refreshToken(): Promise<string> {
const response = await fetch(
"https://na1-prod.okcapsule.app/v2/authentication/refresh",
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ refresh_token }),
}
);

const data = await response.json();
return data.access_token;
}

403 Forbidden

{
"statusCode": 403,
"error": "Forbidden",
"message": "Insufficient permissions"
}

Solution: Your user lacks the required permission scope. Contact your admin to update role permissions.

Rate Limiting

When rate limited, the API returns:

HTTP/1.1 429 Too Many Requests
Retry-After: 60

Best practices:

  • Implement exponential backoff
  • Cache responses when possible
  • Batch operations where supported
async function withRetry<T>(fn: () => Promise<T>, maxRetries = 3): Promise<T> {
for (let attempt = 0; attempt < maxRetries; attempt++) {
try {
return await fn();
} catch (error) {
if (error.status === 429 && attempt < maxRetries - 1) {
const delay = Math.pow(2, attempt) * 1000; // 1s, 2s, 4s
await sleep(delay);
continue;
}
throw error;
}
}
throw new Error("Max retries exceeded");
}

Debugging Tips

  1. Check the full error response - Details array contains field-specific errors
  2. Validate before sending - Use the field constraints from Create Orders
  3. Test in Stage first - Use the stage environment for development
  4. Log request/response - Include correlation IDs for support tickets

Getting Help

If you encounter persistent errors:

  1. Check the Interactive API Docs for endpoint details
  2. Verify your request matches the expected schema
  3. Contact OK Capsule support with:
    • Request URL and method
    • Request body (redact sensitive data)
    • Full error response
    • Timestamp of the request