📋 Code Snippet Manager — API Documentation

Error Handling

All errors return a consistent JSON envelope:

{
  "error": {
    "code": "VALIDATION_FAILED",
    "message": "Field 'language' is required",
    "details": [
      { "field": "language", "issue": "missing_required_field" }
    ],
    "request_id": "req_9f2k1m"
  }
}

Always log request_id when contacting support.

HTTP status codes

Status Meaning Retry?
200 Success -
201 Resource created -
204 Deleted, no body -
400 Malformed request / validation error No
401 Missing or invalid token No (re-auth)
403 Authenticated but not allowed No
404 Resource not found No
409 Conflict (duplicate title/slug) No
422 Semantically invalid payload No
429 Rate limit exceeded Yes (backoff)
500 Internal server error Yes
503 Service unavailable / maintenance Yes

Application error codes

Code HTTP Description
AUTH_TOKEN_MISSING 401 No Authorization header
AUTH_TOKEN_EXPIRED 401 JWT expired, refresh needed
AUTH_TOKEN_INVALID 401 Signature verification failed
FORBIDDEN_NOT_OWNER 403 Snippet belongs to another user
SNIPPET_NOT_FOUND 404 Unknown snippet ID
VALIDATION_FAILED 400 One or more fields invalid
LANGUAGE_UNSUPPORTED 422 Language not in /languages
TAG_LIMIT_EXCEEDED 422 More than 20 tags per snippet
CONTENT_TOO_LARGE 422 Snippet exceeds 256 KB
DUPLICATE_SNIPPET 409 Identical title + content exists
RATE_LIMIT_EXCEEDED 429 Quota exhausted
PLAN_LIMIT_REACHED 403 Free tier snippet cap hit
INTERNAL_ERROR 500 Unexpected server failure

Rate limit headers

Every response includes:

X-RateLimit-Limit: 1000
X-RateLimit-Remaining: 873
X-RateLimit-Reset: 1768473600
Retry-After: 42        # only on 429

Retry with exponential backoff (Python)

import time
import requests

def request_with_retry(method, url, max_attempts=3, **kwargs):
    delay = 1.0
    for attempt in range(1, max_attempts + 1):
        resp = requests.request(method, url, timeout=(10, 30), **kwargs)
        if resp.status_code in (429, 500, 502, 503, 504):
            if attempt == max_attempts:
                resp.raise_for_status()
            wait = float(resp.headers.get("Retry-After", delay))
            time.sleep(wait)
            delay *= 2
            continue
        resp.raise_for_status()
        return resp
    raise RuntimeError("unreachable")

Retry with backoff (JavaScript)

async function fetchWithRetry(url, options = {}, maxAttempts = 3) {
  let delay = 1000;
  for (let attempt = 1; attempt <= maxAttempts; attempt++) {
    const res = await fetch(url, options);
    const retryable = [429, 500, 502, 503, 504].includes(res.status);
    if (!retryable) return res;
    if (attempt === maxAttempts) return res;
    const retryAfter = Number(res.headers.get('Retry-After')) * 1000;
    await new Promise(r => setTimeout(r, retryAfter || delay));
    delay *= 2;
  }
}

Troubleshooting checklist

  1. 401 right after login → token not prefixed with Bearer, or clock skew > 5 min.
  2. 403 FORBIDDEN_NOT_OWNER → you are editing another user's private snippet.
  3. 422 LANGUAGE_UNSUPPORTED → fetch /languages and use the exact id.
  4. 429 on bulk import → throttle to ≤ 5 req/s and honor Retry-After.
  5. Empty data: [] but snippets exist → check is_public filter and pagination params.