Skip to navigation

REST Client

TypeScript API reference for RestClient and resource namespaces
View as MarkdownOpen in Claude

The REST namespace provides an HTTP client for the SignalWire platform APIs. It organizes every HTTP endpoint into namespaced resource objects with standard CRUD operations, letting you manage phone numbers, fabric resources, call logs, video rooms, datasphere documents, and more from TypeScript.

Example

Search for available phone numbers, purchase one, and assign it to a fabric AI agent resource:

import { RestClient } from "@signalwire/sdk";
const client = new RestClient({
project: "your-project-id",
token: "your-api-token",
host: "your-space.signalwire.com"
});
// Search for available numbers in area code 512
const available = await client.phoneNumbers.search({ areacode: "512", max_results: 3 });
for (const number of available.data ?? []) {
console.log(`${number.e164} - ${number.region}`);
}
// Purchase the first available number
const first = available.data?.[0];
if (first) {
const purchased = await client.phoneNumbers.create({ number: first.e164 });
console.log(`Purchased: ${purchased.number}`);
}
// List your AI agent resources
const response = await client.fabric.aiAgents.list();
for (const agent of response.data ?? []) {
console.log(`Agent: ${agent.display_name} (${agent.id})`);
}
// Query recent voice call logs
const logs = await client.logs.voice.list({ page_size: 5 });
for (const log of logs.data ?? []) {
if ('from' in log) {
console.log(`Call from ${log.from} to ${log.to}`);
}
}

All three constructor arguments can also be provided via environment variables: SIGNALWIRE_PROJECT_ID, SIGNALWIRE_API_TOKEN, and SIGNALWIRE_SPACE (or SIGNALWIRE_REST_BASE_URL for a full base URL, which takes precedence). When those are set, you can instantiate with new RestClient() and no arguments.

Pagination

list() returns one page. paginate() returns an async iterator that follows the server’s next-page links and yields one item at a time, so you don’t build the page loop yourself. Query parameters apply to the first request only.

import { RestClient } from "@signalwire/sdk";
const client = new RestClient();
for await (const address of client.fabric.addresses.paginate()) {
console.log(address.name);
}

The standalone paginate() and paginateAll() functions do the same for any list path, given an HttpClient. paginateAll() collects every page into one array, so it holds the whole result set in memory; iterate with paginate() for large ones. Both take the client, the path, query parameters for the first request, and the key that holds each page’s items (default data). paginate() also takes request options, applied to every page.

import { HttpClient, paginateAll } from "@signalwire/sdk";
const http = new HttpClient({
project: "your-project-id",
token: "your-api-token",
host: "your-space.signalwire.com",
});
const addresses = await paginateAll<{ id: string; name: string }>(
http,
"/api/fabric/addresses",
);
console.log(`${addresses.length} addresses`);

Timeouts, retries, and cancellation

Every resource method accepts a trailing requestOptions object that sets the timeout, retry count, backoff, and an AbortSignal for that one call. Pass the same object to the constructor to set a client-wide default. See RequestOptions.

import { RestClient } from "@signalwire/sdk";
const client = new RestClient({ requestOptions: { timeout: 10, retries: 2 } });
const controller = new AbortController();
const numbers = await client.phoneNumbers.list(
undefined,
{ abortSignal: controller.signal },
);

Error handling

REST errors throw RestError. A request that never reaches the server, such as a DNS failure or a timeout, throws RestTransportError, a subclass of RestError with a null status code, so one catch handles both:

import { RestClient, RestError } from "@signalwire/sdk";
const client = new RestClient();
try {
await client.phoneNumbers.get("nonexistent-id");
} catch (e) {
if (e instanceof RestError) {
console.log(`HTTP ${e.statusCode}: ${e.body}`);
console.log(`URL: ${e.method} ${e.url}`);
console.log(`Request ID: ${e.requestId}`);
}
}

Resources