Kenso Python SDK
The official Python client for the Kenso API. It has one typed function per API operation, generated from Kenso's public OpenAPI document, and it 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 TypeScript or JavaScript? Use the Kenso TypeScript SDK.
Install
pip install kensoPython 3.10 or newer. This version is sync only. It is kenso on PyPI.
Quickstart
Create an API key in Kenso under Settings > API keys. Keys start with kenso_live_ or, in the older format, ken_.
from kenso import Kenso
from kenso.api.campaigns import campaigns_list
from kenso.api.me import me_get
with Kenso("your_api_key", workspace_id=812) as kenso:
me = me_get.sync(client=kenso)
page = campaigns_list.sync(client=kenso, limit=20)Every operation is a module under kenso.api.<section> with a sync function that returns the parsed response, and a sync_detailed function that also returns the status code and headers. Models are in kenso.models. Pass the Kenso client as client=.
workspace_id is sent as X-Ken-Workspace-Id. Leave it out when your key reaches only one workspace.
Pagination
paginate calls a list function page by page and yields every item. The arguments after the function are the function's own, and a type checker checks them.
from kenso import paginate
for campaign in paginate(campaigns_list.sync, client=kenso, status="active"):
print(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 raises a KensoError. Branch on the class, or on code, the API's stable error.code.
from kenso import KensoError, NotFoundError, RateLimitError
from kenso.api.campaigns import campaigns_get
try:
campaign = campaigns_get.sync(4518, client=kenso)
except NotFoundError:
campaign = None
except RateLimitError as error:
print("retry in", error.retry_after, "seconds")
except KensoError as error:
print(error.status, error.code, error.message, error.request_id)| 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 raises the class for its status (a 409 import_running is a ConflictError), or KensoError itself; kenso.ERROR_CODES lists every code the reference names. Quote request_id when you write to support. A request that never gets a response raises KensoConnectionError.
Retries and idempotency
The client retries up to 2 times (max_retries), waiting the Retry-After seconds when the response carries them and otherwise 0.5 s, then 1 s, with jitter.
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, withretry_after.- 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 as idempotency_key= to a function whose 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:
from kenso.api.threads import threads_mark_all_read
threads_mark_all_read.sync(client=kenso, idempotency_key="mark-read-2026-11-15")Webhooks
verify_webhook 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.
from kenso import WebhookVerificationError, verify_webhook
try:
event = verify_webhook(raw_body, request_headers, "whsec_your_signing_secret")
except WebhookVerificationError:
event = None # answer 400It rejects a delivery whose timestamp is more than 5 minutes from now (tolerance_seconds), 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 (kenso.API_VERSION), so a new API version never changes the shapes this package parses. Pass version= to send another. See Versioning.
Good to know
- Enum fields are plain
str. Kenso adds enum values over time, and a closed enum would reject a value this package has not seen. Each field's docstring lists the values known when the package was built. - The client uses
HTTPS_PROXY,HTTP_PROXYandNO_PROXYfrom the environment, as httpx does. - Fields the API leaves out are
UNSET(kenso.types.UNSET), notNone. kenso.apiandkenso.modelsare 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.