Kenso TypeScript SDK
The official TypeScript and JavaScript client for the Kenso API. Every path, parameter, request body and response is typed from Kenso's public OpenAPI document, and the client handles the parts every integration otherwise writes by hand: the key, version and workspace headers, paging through lists, safe retries, typed errors and webhook signature checks. Writing Python? Use the Kenso Python SDK.
Install
npm install kenso-sdkNode 20.3 or newer. The package is ESM and ships its own types. It is kenso-sdk on npm.
Quickstart
Create an API key in Kenso under Settings > API keys. Keys start with kenso_live_ or, in the older format, ken_.
import { Kenso } from "kenso-sdk";
const kenso = new Kenso({ apiKey: "your_api_key", workspaceId: 812 });
const me = await kenso.GET("/v1/me");
const page = await kenso.GET("/v1/campaigns", { params: { query: { limit: 20 } } });
const campaign = await kenso.GET("/v1/campaigns/{campaignId}", { params: { path: { campaignId: 4518 } } });GET, POST, PUT, PATCH and DELETE take a path written as in the API reference and an init with its params.path, params.query and body. Your editor completes the paths and checks every field. A call resolves to the response body ({ success, data }, and pagination on a list), or to undefined for a 204.
workspaceId is sent as X-Ken-Workspace-Id. Leave it out when your key reaches only one workspace.
Pagination
paginate requests a list page by page and yields every item. It accepts only the paths that page with a cursor, and the item type is the list's own.
for await (const campaign of kenso.paginate("/v1/campaigns", { params: { query: { status: "active" } } })) {
console.log(campaign.id, campaign.name);
}It sends each page's nextCursor as cursor and stops when hasMore is false.
Errors
A response with a 4xx or 5xx status rejects with a KensoError. Branch on the class, or on code, the API's stable error.code.
import { KensoError, NotFoundError, RateLimitError } from "kenso-sdk";
try {
await kenso.GET("/v1/campaigns/{campaignId}", { params: { path: { campaignId: 4518 } } });
} catch (error) {
if (error instanceof NotFoundError) {
// the campaign does not exist, or it is in a workspace this key cannot reach
} else if (error instanceof RateLimitError) {
console.log("retry in", error.retryAfter, "seconds");
} else if (error instanceof KensoError) {
console.log(error.status, error.code, error.message, error.requestId);
} else {
throw error;
}
}| Class | `error.code` |
|---|---|
ValidationError | validation_failed (details names the field) |
WorkspaceRequiredError | workspace_required |
VersionError | unsupported_version, version_retired |
AuthenticationError | unauthenticated, and any 401 |
PlanLimitError | plan_limit_reached |
PermissionDeniedError | permission_denied, forbidden |
NotFoundError | not_found |
ConflictError | conflict, invalid_state |
IdempotencyKeyReusedError | idempotency_key_reused |
RateLimitError | rate_limited, quota_exceeded, and any 429 |
ServiceUnavailableError | service_unavailable, and any 503 |
InternalServerError | internal_error, and any other 5xx |
A code with no class of its own rejects with the class for its status (a 409 import_running is a ConflictError), or with KensoError itself; ERROR_CODES lists every code the reference names. Quote requestId when you write to support. A request that never gets a response rejects with KensoConnectionError, which keeps the failure as cause.
Retries and idempotency
The client retries up to 2 times (maxRetries), waiting the Retry-After seconds when the response carries them and otherwise 0.5 s, then 1 s, with jitter. timeoutMs (60 seconds) ends one attempt, the download of its response included, and a timeout before the response starts counts as a connection failure. Raise it for a large export.
429is always retried, because Kenso refused the request before running it. WhenRetry-Afteris over 60 seconds (a daily quota) it is not: theRateLimitErrorreaches you at once, withretryAfter.- A
5xx, a timeout or a connection failure is retried on aGET, and on a write only when you pass anIdempotency-Keyon an operation the API reference lists it for, because the key makes the repeat safe: when the first attempt did complete, Kenso answers with the response it stored and does not run the request again, and when it failed with a5xxit was not completed, so the retry runs it under the same key. Any other write is sent once: after a503on a reply send, for example, the message may already have gone out.
The client never makes up an Idempotency-Key, because a new key on each run would turn off Kenso's own check for a LinkedIn, WhatsApp or Instagram reply sent again shortly after with the same text. Pass your own in params.header on an operation the API reference lists it for, and the same one when your code runs a failed call again: Kenso answers the repeat instead of doing it twice, and the client's own retries reuse it. Kenso ignores the header on every other operation, so adding it there does not make a write safe to repeat, and the client does not retry it:
await kenso.POST("/v1/threads/mark-all-read", {
params: { header: { "Idempotency-Key": "mark-read-2026-11-15" } },
});Webhooks
verifyWebhook checks a delivery's signature and age, and returns the parsed payload. Pass the raw request body and the request headers, with the signing secret (whsec_...). It reads a webhook endpoint's svix-id, svix-timestamp and svix-signature headers, and the webhook-id, webhook-timestamp and webhook-signature headers an event subscription or a Studio webhook step sends.
import { verifyWebhook, WebhookVerificationError } from "kenso-sdk";
try {
const event = verifyWebhook({ payload: rawBody, headers: request.headers, secret: "whsec_your_signing_secret" });
console.log(event.eventType);
} catch (error) {
if (!(error instanceof WebhookVerificationError)) throw error;
// answer 400
}It rejects a delivery whose timestamp is more than 5 minutes from now (toleranceSeconds), and it rejects every delivery when the secret is empty, so an unset secret never verifies anything. Events and their payloads: Webhooks.
Versions
The client sends Ken-Version with the API version it was generated against (API_VERSION), so a new API version never changes the shapes this package types. Pass version to send another. See Versioning.
Good to know
- Pass
signalininitto cancel a call:kenso.GET(path, { signal }). It also ends a wait between retries. - The types are generated on every build from the same OpenAPI document as the API reference, so they match the reference of the day the version was built.
paths,componentsandoperationsare exported for your own helpers:components["schemas"]["Campaign"]. - Enum fields are typed as their known values. Kenso adds enum values over time, so handle a value you do not know in a
defaultbranch.