Errors

RailwailError

All API errors throw a RailwailError with a status code, error type, and human-readable message.

Import

TypeScript
import { RailwailError } from "railwail";

Properties

PropertyTypeDescription
messagestringHuman-readable error message
statusnumberHTTP status code (401, 402, 429, 500, etc.)
typestringError category (e.g. "authentication_error", "rate_limit_error")
codestringSpecific error code (e.g. "invalid_api_key", "insufficient_credits")

Error Handling

TypeScript
import railwail, { RailwailError } from "railwail";

const rw = railwail("rw_live_xxxxx");

try {
  const reply = await rw.run("gpt-4o", "Hello!");
  console.log(reply);
} catch (err) {
  if (err instanceof RailwailError) {
    console.error(`Status: ${err.status}`);     // 401
    console.error(`Type: ${err.type}`);          // "authentication_error"
    console.error(`Code: ${err.code}`);          // "invalid_api_key"
    console.error(`Message: ${err.message}`);    // "Invalid API key provided"

    // Handle specific errors
    switch (err.status) {
      case 401:
        console.error("Check your API key");
        break;
      case 402:
        console.error("Add credits at railwail.com/billing");
        break;
      case 429:
        console.error("Rate limited — retry after cooldown");
        break;
    }
  } else {
    // Network error, timeout, etc.
    console.error("Unexpected error:", err);
  }
}

Common Error Codes

StatusCodeDescription
400bad_requestMalformed request body or missing required fields
401invalid_api_keyInvalid or missing API key
402insufficient_creditsAccount does not have enough credits
429rate_limit_exceededToo many requests — retry after cooldown
500internal_errorServer error — try again later
See the full Error Codes reference for all error types and suggested handling strategies.