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.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.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.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
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, abootstrap or cached snapshot is available, or readyTimeoutMs elapses.
Promise<void>. It never rejects for network errors. Returning on timeout means evaluations use defaults until a snapshot arrives.
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.boolean.
Fallback. See Defaults.
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.
"control" | "semantic" | "hybrid".
Fallback. See Defaults.
useVariant · Run an A/B test
getNumber
Returns the value of a number flag.number. A non-finite served value falls back to defaults[key].
useNumber · Number and JSON flags
getJson
Returns the value of a JSON flag, typed as its declared default.default in shipd.config.ts; the SDK does not validate the served value’s shape.
useJson
identify
Sends anidentify 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.
shipdit: identify requires userId when userId is empty.
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, ornull.
subscribe
Callslistener when a new snapshot applies or the identified user changes.
getVersion
Returns the loaded snapshot’s version, ornull before the first snapshot.
getRevision
Returns a counter that increases whenever a snapshot applies or the identity changes. Use it withuseSyncExternalStore.
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.
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.createClient({ context })identify({ userId, traits })(browser clients only):traitsbecomeattributes- The
contextargument 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 yourFlagDefinitions. Call it once, in a client-only module.
src/lib/shipd-react.ts
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
Returnstrue once client.ready() has resolved.
useShipdClient
Returns the provided client.ShipdProvider.
Version constants
Related
- Local evaluation: the refresh path and failure behavior
- Edge API: the HTTP and WebSocket routes the SDK calls
- Errors:
onErrormessages