This guide explains how to handle errors when using Sim APIs, including common error codes, troubleshooting steps, and code examples for proper error handling.

Error Response Format

When an error occurs, Sim APIs return a JSON response with error information:

{
  "error": "Description of what went wrong"
}

The error property can be either "error" or "message" depending on the type of error.

Common Error Codes

HTTP StatusDescriptionTroubleshooting
401Invalid or missing API keyCheck that you’re including the correct API key in the X-Sim-Api-Key header
400Malformed requestVerify the address format and other parameters in your request
404Resource not foundVerify the endpoint URL and resource identifiers
429Too many requestsImplement backoff strategies and consider upgrading your plan if you consistently hit limits
500Server-side errorRetry the request after a short delay; if persistent, contact support

Handling Errors in Code

Here are examples of how to properly handle errors in different programming languages:

fetch('https://api.sim.dune.com/v1/evm/balances/0xd8da6bf26964af9d7eed9e03e53415d37aa96045', {
  method: 'GET', 
  headers: {'X-Sim-Api-Key': 'YOUR_API_KEY'}
})
  .then(response => {
    if (!response.ok) {
      return response.json().then(err => {
        const errorMessage = err.error || err.message || response.statusText;
        throw new Error(`API error: ${errorMessage}`);
      });
    }
    return response.json();
  })
  .then(data => {
    console.log('Success:', data);
    // Process your data here
  })
  .catch(err => {
    console.error('Error fetching balances:', err);
    // Handle error appropriately in your application
    // e.g., show user-friendly message, retry, or fallback behavior
  });

Best Practices for Error Handling

  1. Always check for errors: Don’t assume API calls will succeed.

  2. Use HTTP status codes: Rely on HTTP status codes rather than parsing error message strings for programmatic decisions.

  3. Implement retry logic with backoff: For transient errors (like rate limits or server errors), implement exponential backoff.

  4. Provide meaningful error messages: Transform API error responses into user-friendly messages.

  5. Log errors for debugging: Maintain detailed logs of API errors for troubleshooting.

  6. Implement fallbacks: When possible, have fallback behavior when API calls fail.

Debugging Tips

If you’re experiencing persistent errors:

  1. Verify your API key: Ensure it’s valid and has the necessary permissions.

  2. Check request format: Validate that your request parameters match the API specifications.

  3. Inspect full error responses: The error message often contains specific details about what went wrong.

  4. Monitor your usage: Check if you’re approaching or exceeding rate limits.

  5. Test with cURL: Isolate issues by testing the API directly with cURL:

    curl -v -X GET "https://api.sim.dune.com/v1/evm/balances/0xd8da6bf26964af9d7eed9e03e53415d37aa96045" \
         -H "X-Sim-Api-Key: YOUR_API_KEY"
    

Need More Help?

If you’re still experiencing issues after following these guidelines, please reach out through our support channels.