Skip to main content

API Documentation

Everything you need to integrate StockAlert.pro into your applications

Error Handling

How to handle errors and troubleshoot issues with the API

Error Response Format

All error responses from the StockAlert.pro API follow a consistent JSON format:

{
  "success": false,
  "error": "A human-readable error message",
  "code": "ERROR_CODE",
  "details": {
    // Additional error context (optional)
  }
}

Response Fields

  • success - Always false for error responses
  • error - Human-readable error message
  • code - Machine-readable error code (optional)
  • details - Additional context about the error (optional)

HTTP Status Codes

4xx Client Errors

400
Bad Request

The request was invalid. Check your parameters and request body.

{
  "success": false,
  "error": "Invalid stock symbol format",
  "code": "INVALID_PARAMETER"
}
401
Unauthorized

Authentication failed. Check your API key.

{
  "success": false,
  "error": "Invalid API key",
  "code": "INVALID_API_KEY"
}
403
Forbidden

You don't have permission to access this resource.

{
  "success": false,
  "error": "Alert limit reached for your account",
  "code": "LIMIT_EXCEEDED"
}
404
Not Found

The requested resource doesn't exist.

{
  "success": false,
  "error": "Alert not found",
  "code": "RESOURCE_NOT_FOUND"
}
429
Too Many Requests

Rate limit exceeded. See Rate Limits documentation.

{
  "success": false,
  "error": "Rate limit exceeded. Please retry after 60 seconds",
  "code": "RATE_LIMIT_EXCEEDED",
  "details": {
    "limit": 10,
    "remaining": 0,
    "reset": 1704456000
  }
}

5xx Server Errors

500
Internal Server Error

Something went wrong on our end. Please try again later.

{
  "success": false,
  "error": "Internal server error",
  "code": "INTERNAL_ERROR"
}
503
Service Unavailable

The service is temporarily unavailable. Please retry.

{
  "success": false,
  "error": "Service temporarily unavailable",
  "code": "SERVICE_UNAVAILABLE"
}

Common Error Codes

Error CodeDescription
INVALID_PARAMETEROne or more parameters are invalid
MISSING_PARAMETERA required parameter is missing
INVALID_API_KEYThe API key is invalid or expired
RATE_LIMIT_EXCEEDEDToo many requests in a given time period
RESOURCE_NOT_FOUNDThe requested resource doesn't exist
LIMIT_EXCEEDEDAccount limit reached (alerts, API keys, etc.)
INVALID_STOCK_SYMBOLStock symbol not found or not supported
DUPLICATE_ALERTAn identical alert already exists

Best Practices

Handling Errors in Your Code

JavaScript/TypeScript Example
async function createAlert(alertData) {
  try {
    const response = await fetch('https://stockalert.pro/api/public/v1/alerts', {
      method: 'POST',
      headers: {
        'X-API-Key': process.env.API_KEY,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(alertData)
    });

    const data = await response.json();

    if (!response.ok) {
      // Handle specific error codes
      switch (data.code) {
        case 'RATE_LIMIT_EXCEEDED':
          // Wait and retry
          const retryAfter = data.details?.reset || 60;
          await sleep(retryAfter * 1000);
          return createAlert(alertData); // Retry
        
        case 'INVALID_STOCK_SYMBOL':
          // Show user-friendly error
          throw new Error('Stock symbol not found. Please check and try again.');
        
        default:
          // Generic error handling
          throw new Error(data.error || 'An error occurred');
      }
    }

    return data;
  } catch (error) {
    console.error('API Error:', error);
    throw error;
  }
}
Python Example
import requests
import time

def create_alert(alert_data, max_retries=3):
    url = 'https://stockalert.pro/api/public/v1/alerts'
    headers = {
        'X-API-Key': os.environ['API_KEY'],
        'Content-Type': 'application/json'
    }
    
    for attempt in range(max_retries):
        try:
            response = requests.post(url, json=alert_data, headers=headers)
            data = response.json()
            
            if response.status_code == 429:
                # Rate limited - wait and retry
                retry_after = data.get('details', {}).get('reset', 60)
                time.sleep(retry_after)
                continue
            
            if not response.ok:
                # Handle specific errors
                error_code = data.get('code')
                if error_code == 'INVALID_STOCK_SYMBOL':
                    raise ValueError('Invalid stock symbol')
                else:
                    raise Exception(data.get('error', 'Unknown error'))
            
            return data
            
        except requests.exceptions.RequestException as e:
            print(f"Request failed: {e}")
            if attempt == max_retries - 1:
                raise
    
    raise Exception("Max retries exceeded")

General Guidelines

  • Always check the response status - Don't assume success
  • Parse error details - Use the error code for specific handling
  • Implement retry logic - For transient errors (5xx, rate limits)
  • Log errors - Include request ID for support inquiries
  • Show user-friendly messages - Don't expose raw API errors to users