Chatspark
K

API Reference

v1
K

Errors

The API returns consistent error responses with a code, message, and optional details. Use the x-request-id header when contacting support for debugging.

Error Response Format

All errors follow this envelope:

json

{
  "error": {
    "code": "VALIDATION_ERROR",
    "message": "Invalid request parameters.",
    "details": {
      "fields": {
        "message": ["must be a non-empty string"]
      }
    }
  }
}

The details object is optional and may include field-level validation errors.

Error Codes

Full reference of all error codes, HTTP status, and descriptions:

CodeHTTP StatusDescription
API_KEY_MISSING401Missing Authorization header. Use: Authorization: Bearer cs_live_...
API_KEY_INVALID401Invalid API key. Check your key and try again.
API_KEY_EXPIRED401API key has expired. Generate a new key from your dashboard.
API_KEY_REVOKED401API key has been revoked.
API_KEY_SCOPE_INSUFFICIENT403API key lacks the required scope for this operation.
PLAN_UPGRADE_REQUIRED403Your plan does not include access to this endpoint. Upgrade at chatspark.io/pricing
TEAM_ROLE_INSUFFICIENT403Your team role does not permit this operation.
AGENT_ACCESS_DENIED403This API key is restricted to specific agents and does not include the requested agent.
RATE_LIMIT_EXCEEDED429Rate limit exceeded. Check X-RateLimit-* headers for details.
AGENT_NOT_FOUND404Agent not found.
RESOURCE_NOT_FOUND404The requested resource was not found.
VALIDATION_ERROR400Invalid request parameters.
IDEMPOTENCY_CONFLICT409Idempotency-Key was already used with different request parameters.
CREDITS_EXHAUSTED402Monthly message limit reached.
STREAMING_ERROR500An error occurred during response streaming.
INTERNAL_ERROR500An internal server error occurred.

Debugging with x-request-id

Every response includes an x-request-id header, a unique identifier for the request. Include it when reporting issues or contacting support to help us trace the request.

javascript

const res = await fetch("https://chatspark.io/api/v1/agents/ag_123/chat", {
  method: "POST",
  headers: {
    "Authorization": "Bearer cs_live_...",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ message: "Hello" }),
});

const requestId = res.headers.get("x-request-id");
const data = await res.json();

if (!res.ok) {
  console.error(`Request ${requestId} failed:`, data.error);
  // Use requestId when contacting support
}

Handling Errors in JavaScript

A practical example of parsing and handling API errors:

javascript

async function sendMessage(agentId, message) {
  const res = await fetch(`https://chatspark.io/api/v1/agents/${agentId}/chat`, {
    method: "POST",
    headers: {
      "Authorization": `Bearer ${process.env.CHATSPARK_API_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ message }),
  });

  const data = await res.json();
  const requestId = res.headers.get("x-request-id");

  if (!res.ok) {
    const { code, message, details } = data.error || {};
    switch (code) {
      case "API_KEY_MISSING":
      case "API_KEY_INVALID":
      case "API_KEY_EXPIRED":
      case "API_KEY_REVOKED":
        throw new Error("Authentication failed: " + message);
      case "RATE_LIMIT_EXCEEDED":
        const retryAfter = res.headers.get("Retry-After") || 60;
        throw new Error(`Rate limited. Retry after ${retryAfter}s (request-id: ${requestId})`);
      case "CREDITS_EXHAUSTED":
        throw new Error("Monthly message limit reached. Upgrade your plan.");
      case "VALIDATION_ERROR":
        throw new Error("Invalid request: " + (details?.fields ? JSON.stringify(details.fields) : message));
      default:
        throw new Error(`API error: ${code || res.status} - ${message} (request-id: ${requestId})`);
    }
  }

  return data;
}

Previous

Rate Limits

Next

Pagination