REST API v2
Best practices & security

Best practices & security

Best practices

Paginate efficiently

// Use hasCount sparingly to avoid performance impact
const customers = await fetch('/rest_api/v2/customers?hasCount=false');
 
// Navigate using document IDs, not page numbers
const nextPage = await fetch(`/rest_api/v2/customers?after=${lastCustomerId}`);

See Pagination & filtering.

Query date ranges in ISO 8601

// Use proper ISO 8601 format for dates
const recentCustomers = await fetch(
  '/rest_api/v2/customers?created_at_min=2023-07-01T00:00:00Z'
);

Handle errors

async function handleApiCall(url) {
  const response = await fetch(url);
  const data = await response.json();
 
  if (!data.success) {
    throw new Error(`${data.error.code}: ${data.error.message}`);
  }
 
  return data.data;
}

See Errors for every status, code and body shape.

Use bulk operations

// Instead of multiple single requests (inefficient)
customers.forEach(id => updateCustomer(id));
 
// Use bulk operations when available (efficient)
await updateMultipleCustomers(customerIds);

Security

Protect your API keys

  • Use HTTPS only for all API requests.
  • Store credentials securely in environment variables, never in version control.
  • Never expose credentials in client-side code.
  • Rotate credentials regularly, with a key rotation policy.
  • Monitor for unauthorized usage and suspicious activity.

Protect customer data

  • Customer emails and personal data are included in responses.
  • Ensure compliance with privacy regulations (GDPR, CCPA).
  • Implement proper access controls in your application.

Validate input

  • Validate all input data on your end.
  • Use proper data types for API calls.
  • Sanitize user input before sending to the API.

Handle error information safely

  • Error responses may contain sensitive information.
  • Log errors securely without exposing them to end users.
  • Implement proper error boundaries.