Standard Response Object

Understanding the consistent response format used across all Finatic SDK methods.

All Finatic SDK methods return a consistent, predictable response format. This standardization makes error handling and data access straightforward across your application.

Response Structure

Every SDK method returns an object with three properties:

1{ 2 success: { 3 data: { ... }, 4 meta: { ... } 5 }, 6 error: { ... } | null, 7 warning: { ... } | null 8} 9

At any given time:

  • Either success or error will be populated, never both
  • warning may be present alongside either success or error

Success Response

When a request succeeds, the success property contains your data:

1{ 2 success: { 3 data: { /* your data */ }, 4 meta?: { /* optional metadata */ } 5 }, 6 error: null, 7 warning: null 8} 9

Success Properties

  • data: The actual response data (accounts, orders, positions, etc.)
  • meta (optional): Additional metadata about the response (pagination info, timestamps, etc.)

Success Example

1const result = await finatic.getAccounts(); 2 3if (result.success) { 4 const accounts = result.success.data; 5 console.log('Accounts:', accounts); 6 7 // Access metadata if available 8 if (result.success.meta) { 9 console.log('Metadata:', result.success.meta); 10 } 11}

Error Response

When a request fails, the error property contains details about what went wrong:

1{ 2 success: null, 3 error: { 4 message: "Error description", 5 code?: "ERROR_CODE", 6 status?: 400, 7 details?: { /* additional error details */ } 8 }, 9 warning: null 10} 11

Error Properties

  • message: Human-readable error description (always present)
  • code (optional): Machine-readable error code for programmatic handling
  • status (optional): HTTP status code (e.g., 400, 401, 404, 500)
  • details (optional): Additional context about the error (validation errors, affected fields, etc.)

Error Example

1const result = await finatic.getAccounts(); 2 3if (result.error) { 4 console.error('Error:', result.error.message); 5 console.error('Code:', result.error.code); 6 console.error('Status:', result.error.status); 7 8 // Handle specific errors 9 if (result.error.details) { 10 console.error('Details:', result.error.details); 11 } 12}

Warning Messages

Some responses may include warnings alongside successful results:

1{ 2 success: { 3 data: { /* your data */ }, 4 meta?: { /* optional metadata */ } 5 }, 6 error: null, 7 warning: [ 8 { 9 message: "Warning description", 10 code?: "WARNING_CODE" 11 } 12 ] 13} 14

Warnings don't indicate failure but provide important information about:

  • Deprecated features being used
  • Rate limits approaching
  • Data freshness concerns
  • Partial results returned

Warning Example

1const result = await finatic.getAccounts(); 2 3if (result.success) { 4 const accounts = result.success.data; 5 6 // Check for warnings 7 if (result.warning && result.warning.length > 0) { 8 result.warning.forEach(warning => { 9 console.warn('Warning:', warning.message); 10 if (warning.code) { 11 console.warn('Code:', warning.code); 12 } 13 }); 14 } 15}

Basic Response Handling

Always check which property is populated before accessing data:

1const result = await finatic.getAccounts(); 2 3if (result.success) { 4 // Handle success - data is available 5 const accounts = result.success.data; 6 processAccounts(accounts); 7} else if (result.error) { 8 // Handle error - something went wrong 9 console.error('Failed:', result.error.message); 10 showErrorToUser(result.error); 11}

Paginated Data

Methods that return lists (getAccounts(), getOrders(), getPositions()) return paginated data. The data property is a PaginatedData object that behaves like an array but includes navigation methods:

1const result = await finatic.getOrders({ limit: 50 }); 2 3if (result.success) { 4 const orders = result.success.data; // PaginatedData object 5 6 // Use as array 7 console.log(`Found ${orders.length} orders`); 8 orders.forEach(order => console.log(order.symbol)); 9 10 // Check for more pages 11 if (orders.hasMore) { 12 // Navigate to next page 13 const nextPage = await orders.nextPage(); 14 console.log(`Next page: ${nextPage.length} orders`); 15 } 16} 17

For more details on pagination, see Getting Data - Pagination.

Type Safety (TypeScript)

The response format is fully typed in TypeScript:

1import { FinaticResponse } from '@finatic/client-sdk'; 2 3// Response types are inferred 4const result = await finatic.getAccounts(); 5// result is FinaticResponse<Account[]> 6 7if (result.success) { 8 // TypeScript knows data is Account[] 9 const accounts = result.success.data; 10 accounts.forEach(account => { 11 console.log(account.id); // ✓ Type-safe 12 }); 13} 14 15if (result.error) { 16 // TypeScript knows error properties 17 const message: string = result.error.message; 18 const status: number | undefined = result.error.status; 19} 20

Common Status Codes

Understanding HTTP status codes helps with error handling:

StatusMeaningAction
200SuccessData available in success.data
400Bad RequestCheck request parameters
401UnauthorizedRe-authenticate or refresh token
403ForbiddenCheck permissions
404Not FoundResource doesn't exist
422Validation ErrorFix invalid parameters
429Rate LimitedImplement backoff, retry later
500Server ErrorRetry with exponential backoff
503Service UnavailableService temporarily down, retry later

Best Practices

  1. Always check for errors first - Prevents accessing undefined data

    1if (result.error) { 2 handleError(result.error); 3 return; 4} 5// Now safely access result.success 6
  2. Don't assume success - Always verify the response state

    1// ❌ Bad - assumes success 2const accounts = result.success.data; 3 4// ✅ Good - checks first 5if (result.success) { 6 const accounts = result.success.data; 7} 8
  3. Handle warnings - Don't ignore important information

    1if (result.warning) { 2 logWarnings(result.warning); 3} 4
  4. Use type guards - Leverage TypeScript's type narrowing

    1if (result.success) { 2 // TypeScript narrows the type here 3 const data = result.success.data; 4} 5

Next Steps