Skip to main content

Overview

This is the canonical reference for the TypeScript SDK, shipdit. Guides link here for signatures, options, and fallback behavior.
Requires Node.js 20 or later. Realtime updates (stream: true) need a WebSocket implementation. Node.js 22+ and browsers provide a global WebSocket; on Node.js 20, pass a compatible constructor through the WebSocket option. Guarantees. Evaluation methods never throw and never make network requests. Before the first snapshot, and for flags missing from it, they return your defaults.

Types

FlagSnapshot is documented in Snapshots. FlagDefinitions is generated into src/generated/flags.ts; import it from there. The FlagDefinitions type exported by shipdit/config is a different type (the loose authoring map defineFlags accepts), so don’t use it to type a client.

createClient

Creates a client that downloads one environment’s snapshot and evaluates flags against it in memory.
The client fetches the snapshot immediately, then refreshes it every refreshIntervalMs using If-None-Match. With stream: true, it also refreshes as soon as shipd announces a change. Create one client per process (server) or per page (browser) and reuse it.

Options

string
required
An SDK key for one environment: shipd_sdk_server_… on servers, shipd_sdk_client_… in browsers. See Keys and tokens.
PartialDefaults<FlagDefinitions>
Values returned before the first snapshot arrives and for flags missing from the snapshot. Pass one for every flag. See Defaults.
boolean
default:"false"
Open a WebSocket to https://ws.useshipd.com for near-instant updates. Polling continues as a fallback. Needs a global WebSocket (Node.js 22+, browsers) or the WebSocket option.
ClientOverrides
Connection overrides. Production apps should not set these.
EvaluationContext
Base context merged under every evaluation.
number
default:"30000"
Poll interval. Polling continues while the WebSocket is connected. 0 fetches once and never polls.
number
default:"5000"
Maximum time ready() waits for the first snapshot.
number
default:"10000"
Per-request HTTP timeout for snapshot fetches and event flushes. 0 disables the timeout.
FlagSnapshot
A snapshot to evaluate against until the first successful fetch. Use it for server-rendered pages and tests.
SnapshotCache
Durable store for the last good snapshot. Browser clients default to localStorage.
(error: unknown) => void
Receives background fetch, stream, cache, and event-delivery errors. The client keeps working after every error. Messages are listed in Errors.
boolean
default:"true"
Send evaluation, identify, and diagnostic events to POST /sdk/v1/events.
number
default:"50"
Flush when this many events are buffered.
number
default:"5000"
Flush interval. 0 disables timed flushes.
boolean
default:"true"
Browser clients only. After identify(), fetch the user’s last-seen traits from GET /sdk/v1/identity and merge them under the traits you passed.
boolean
Add country, region, city, and timezone under your own attributes. Default true in shipdit/client, off in shipdit.
string
Stable id for a signed-out visitor. Browser clients generate and persist one if omitted.
(anonymousId: string) => void
Called when the client assigns or changes the anonymous id.
WebSocket constructor
WebSocket implementation for runtimes without a global one (for example, ws on Node.js 20).
typeof fetch
fetch implementation. Useful in tests.
Browser-only options (shipdit/client):
boolean
default:"true"
Save the last good snapshot to localStorage and load it on the next page view.
string
localStorage key for the snapshot. Defaults to a key derived from sdkKey and the edge origin.
string
default:"shipdit:anonymousId"
localStorage key for the anonymous id.
Returns a ShipdClient. Errors. Throws synchronously shipdit: sdkKey is required when sdkKey is empty. Removed endpoint or streamEndpoint options and invalid overrides.unsafeEndpoint values also throw synchronously. overrides.streamEndpoint is validated only when stream is enabled; network problems go to onError.
src/lib/shipd.ts
Related: Node.js · Browser · Keys and tokens

Defaults

defaults decides what your users get when shipd can’t answer. The built-in fallbacks exist only so the return type holds. "control" may not be one of your variants. Always pass a default for every flag.

Client methods

ready

Resolves when the first snapshot is loaded, the first fetch fails, a bootstrap or cached snapshot is available, or readyTimeoutMs elapses.
Returns Promise<void>. It never rejects for network errors. Returning on timeout means evaluations use defaults until a snapshot arrives.
Related: Options · useReady

isEnabled

Returns the value of a boolean flag.
BooleanFlagKey
required
A flag whose type is boolean. Other keys are a type error.
EvaluationContext
Merged over the identified user and the client context for this call only.
Returns boolean. Fallback. See Defaults.
Errors. Never throws. Related: useFlag · Create a boolean flag

getVariant

Returns the value of a variant flag, typed as the union of its variants.
VariantFlagKey
required
A flag whose type is variant.
EvaluationContext
Per-call context.
Returns one of the flag’s variants, for example "control" | "semantic" | "hybrid". Fallback. See Defaults.
Errors. Never throws. Related: useVariant · Run an A/B test

getNumber

Returns the value of a number flag.
Returns a finite number. A non-finite served value falls back to defaults[key].
Errors. Never throws. Related: useNumber · Number and JSON flags

getJson

Returns the value of a JSON flag, typed as its declared default.
Returns the served JSON value. The type comes from the default in shipd.config.ts; the SDK does not validate the served value’s shape.
Errors. Never throws. Related: useJson

identify

Sends an identify event. In browsers (shipdit/client), it also sets the user for every later evaluation on the client. On servers (shipdit), it only sends the event and never changes evaluation context.
string
required
Your stable user id. Used for targeting and percentage bucketing.
Record<string, string | number | boolean | null>
Attributes available to targeting rules, such as plan or email.
string
Display name shown in the dashboard. Also available to rules as the name attribute.
string
Links this user to a signed-out session.
Errors. Throws shipdit: identify requires userId when userId is empty.
A server client handles many users, so pass context on each call. Server identify() records the user’s traits for audience insights without affecting other requests. Related: Identify users

reset

Clears the identified user and starts a new anonymous id in browser clients. Call it on logout. Server clients (shipdit) treat reset() as a no-op: it does not change the anonymous id or notify subscribers.

getAnonymousId

Returns the anonymous id used to connect signed-out and signed-in sessions, or null.

subscribe

Calls listener when a new snapshot applies or the identified user changes.
Returns an unsubscribe function.

getVersion

Returns the loaded snapshot’s version, or null before the first snapshot.

getRevision

Returns a counter that increases whenever a snapshot applies or the identity changes. Use it with useSyncExternalStore.

close

Stops polling and the WebSocket, and flushes pending events. Call it on shutdown. Evaluation keeps serving the last snapshot afterward.
number
default:"5000"
How long to wait for pending events. When it passes, pending sends are aborted and the promise resolves.
The promise resolves once pending events are delivered, delivery fails, or timeoutMs passes. It never rejects. Events recorded while a send is in flight are delivered too, in batches of at most 100.

Evaluation context merge

The context used for a check is built from three layers. Later layers win.
  1. createClient({ context })
  2. identify({ userId, traits }) (browser clients only): traits become attributes
  3. The context argument on the call
userId and segmentIds are replaced. attributes merge key by key. In browsers, automatic timezone and location traits sit underneath all three.

React

createShipdReact

Binds typed hooks and a provider to your FlagDefinitions. Call it once, in a client-only module.
src/lib/shipd-react.ts
Use hooks from the same createShipdReact call as the provider. Requires React 18 or later. Related: React · Next.js

ShipdProvider

Makes a client available to hooks below it.
ShipdClient
required
A client from shipdit/client.

useFlag

isEnabled as a hook. Re-renders when a snapshot applies or the identity changes.

useVariant

useNumber

useJson

useReady

Returns true once client.ready() has resolved.

useShipdClient

Returns the provided client.
Errors. Throws when used outside ShipdProvider.

Version constants