HttpClient is the thin fetch wrapper underneath every resource method. It:
- Injects the auth headers (
Authorization: Bearer <credential>, plusx-persona-external-user-idwhen set). - Serializes the body (JSON for plain objects, multipart for
FormData). - Builds the URL from
baseUrl + path+ query params. - Parses the
{success, data}envelope and returns thedatafield. - Throws a typed
PersonaApiErrorsubclass on any non-2xx or{success:false}response. - Retries 429s using the API’s own
Retry-Afterheader.
PersonaClient builds one internally), so it’s your escape hatch for calling an endpoint
this SDK doesn’t wrap yet.
Constructor
HttpClientOptions is the same shape as PersonaClientOptions (the SDK re-exports
PersonaClientOptions = HttpClientOptions) — see Configuration.
The same synchronous validation applies: baseUrl and credential are required, and the
constructor throws if no fetch implementation is available.
request()
method— the HTTP verb. Only these five are allowed (noHEAD/OPTIONS).path— relative tobaseUrl, e.g./api/v1/developer/agents. Leading/is expected.options— see below.
RequestOptions
Return value
The parseddata field from the API’s {success, data} envelope. Two subtleties:
- Binary/stream responses (e.g. file downloads, or
chat.stream()’s SSE body) don’t come back as JSON:request<Response>()returns the rawResponseobject instead, and the caller works withresponse.body/response.arrayBuffer()directly. - Endpoints without the fuller envelope (e.g.
/whoami, which returns its shape without wrapping indata): if the parsed JSON has nodatakey, the raw body is returned asT.
Errors
Any non-2xx response, or a 2xx with{success:false}, throws via errorFromResponse:
Non-JSON error responses (a proxy error page, etc.) throw
PersonaApiError with code
NON_JSON_ERROR_RESPONSE. See Errors for the full picture.
Calling an unwrapped endpoint
Say the API grows an endpoint the SDK doesn’t have a method for yet. You can call it through the client’s internal HTTP layer — butPersonaClient.http is private. The supported pattern is to
build your own HttpClient (or reuse the exported HttpClient class):
Retry behavior
Only429 responses are retried, up to maxRetries times (default 2), waiting the
Retry-After header’s value in seconds (or 1s when absent) between attempts — a fixed per-attempt
wait, not exponential backoff. Any other status throws immediately. Full detail in
Behavior & edge cases.
When to use HttpClient vs. resource methods
- Use resource methods (
client.agents.create()etc.) for anything the SDK wraps — typed params, typed returns, documented behavior. - Use
HttpClientonly for an endpoint this SDK doesn’t wrap yet. Prefer adding the method to the SDK over scattering raw calls, but the escape hatch keeps you unblocked in the meantime.