Skip to main content

REST requests

Use RESTClient for scripts that do not need gateway events. It does not require Client.connect() or a WebSocket:
In an already connected bot, use client.rest instead. For another instance, discover its origin and use the advertised api_public endpoint. Talos preserves the endpoint’s path prefix and handles /v1.

Paths, query fields, and bodies

Pass the route template, with parameters in a separate object:
Talos encodes parameter values. Do not interpolate IDs into the route or prepend /v1. The method and template must exist in the bundled schema; an unknown route throws before making a request. TypeScript infers required parameters, request bodies, and response types from that schema. JSON bodies are serialized automatically. A FormData body is sent as multipart, with the boundary chosen by the runtime. reason sets the encoded audit-log reason header on operations that support it.

Authentication

The default scheme is Bot. RESTClient also accepts Bearer, Session, and Admin through authScheme; Session sends the raw token without a prefix. Choose the scheme that matches your credential. Whether a route needs authentication comes from the generated metadata and local overrides. auth: false omits the token; auth: true requires one. A custom Authorization header is removed, so configure authentication through the client. Authenticated requests do not follow redirects.

Optional Node HTTP pools

Node applications can inject a transport built on node:http and node:https for JSON API calls. It adds no dependencies and keeps Talos’s request deadlines, rate limits, retry policy, and write ordering:
maxConnections bounds pooled sockets per origin, including idle sockets; the REST concurrency setting also bounds active requests. Responses stream with byte-based backpressure and support gzip, deflate, and Brotli compression. HTTPS certificate verification remains enabled. Multipart bodies, Request objects, and operations that follow redirects use the runtime’s native fetch. For a Client, pass transport.fetch as rest.fetch and disconnect before closing the transport. close() cancels pooled work and closes its sockets; finish native-fetch multipart and discovery work before closing. The default transport continues to be the runtime’s native fetch.

Deadlines and queue capacity

Per-request timeoutMs overrides the client default. signal can cancel a request while it is queued or running. Timeouts and cancellations also bound retry waits. Writes with the same method and concrete path run in order. With the default in-memory store, GET, HEAD, and OPTIONS requests can overlap after the first response establishes rate-limit state. Unknown and expired buckets are probed before admitting another read burst; every read still reserves quota. Custom stores retain sequential reads unless they implement canPipeline(key) and atomic reservations. Different paths can run concurrently up to the configured limit. When pending capacity is full, a new request rejects with RequestQueueFullError rather than waiting in another queue. rest.stats reports pending, active, and waiting counts. Concurrent reads can complete out of order. If a response supplies no rate-limit headers, the in-memory store permits bounded concurrency and learns limits from HTTP 429 responses. Missing headers do not prove that the server has no limit.

Share concurrent GET requests

Enable coalesceGets when multiple parts of a bot request the same data at once:
These overlapping calls use one transport operation and one retry budget. Each caller receives its own cloned response, so changing first cannot change second. Sharing reduces HTTP requests and repeated response parsing; cloning still has a CPU and memory cost proportional to the response size. Sharing is disabled by default. Per-request coalesce: true enables it for one GET; coalesce: false overrides a client’s coalesceGets setting. Only GETs without request bodies can share. Matching requires the same route, full URL (including query order), and effective headers, including authentication. Sharing is scoped to one RESTClient; token changes and different headers keep requests separate. Use it for reads where one result can satisfy overlapping callers. Completed results are never cached by this feature. A later request starts fresh. Each caller keeps its own deadline and cancellation signal, including while queued. Cancelling or timing out one caller leaves the others running; when the last caller leaves, Talos aborts the transport. Every caller counts against maxPendingRequests, while concurrency slots and diagnostics describe transport operations. Writes retain their existing ordering and are never shared.

Retries

Talos retries network failures and HTTP 5xx responses for GET, HEAD, OPTIONS, PUT, and DELETE, using exponential backoff with jitter. It does not retry those failures for POST or PATCH. HTTP 429 is handled for all methods: Talos records the route or global rate limit before releasing the concurrency slot, then retries within maxRetries and the request deadline. Requests already in flight may still complete. The delay comes from retry_after or Retry-After, interpreted as seconds. A failed message send can still have reached the server. Retrying a POST after a network error can create a duplicate. Decide how your application will detect duplicates before adding its own retry loop. The default rate-limit store is local to one REST client. If multiple clients use the same credential, pass a shared MemoryRateLimitStore through rateLimitStore. Multiple processes need coordinated storage; the Node worker example uses IPCRateLimitStore and serveRateLimits() from talos-fluxer/node.

Handle errors

FluxerAPIError exposes status, method, the route template, code when the response supplies one, and body. Network failures and abort reasons are thrown directly. A 401 usually calls for checking the token and scheme; a 403 calls for checking access and permissions. Repeating either unchanged will not fix it.

Observe requests

Set rest.onDiagnostic in Client options, or onDiagnostic directly on a RESTClient. It receives response, retry, and rateLimit records with fields such as method, route template, status, attempt, and delay. Attempts start at zero. The records omit tokens and query values. Callback exceptions do not interrupt the request. For REST-only resource helpers, construct new Resources(() => rest). Its messages, channels, guilds, and other helpers work without a gateway; resource caches will receive API results, but no automatic gateway updates.