Shared SDK: The NinetailedApiClient
Work with events and types common to all Ninetailed implementations.
Last updated
Work with events and types common to all Ninetailed implementations.
Last updated
The Shared SDK exposes Ninetailed data objects, types, and methods applicable across runtimes. The Shared SDK allows you to create an API Client that facilitates constructing and sending Experience API events. It also handles retrying requests and request errors.
Elements of the shared SDK are composed to create the JavaScript SDK, which is then composed into Ninetailed's React-based SDKs.
Want to browse the code? Check out the shared SDK in our open source repository.
Parameter | Type Signature | Description |
---|---|---|
A NinetailedAPIClient
maps one function for each Experience API endpoint. upsertProfile
is also provided to conveniently switch between create and update functions.
The returned response of each method is the same as that of the Experience API; a data structure indicating the complete representation of the profile(s) and the Ninetailed Experiences & variants that the Experience API has selected for the profile(s).
All of profile methods accept request options in addition to the supplied events. These are used to control request timeout & retry behaviour, performance, localization, and location resolution.
Each of the profile methods above accepts an array of events. Event building helper functions facilitate generating the required payload for page
, track
, and identify
events. These builder methods take care of parsing the supplied ctx.url
and populating context
properties on events commonly used by Audience rules, including context.page
and context.campaign
.
This is a short example of using the buildPageEvent
helper to create a well-formatted event of type page
and using it to upsert a profile.
Despite the brevity of this example, this is all it takes to get started working with Ninetailed in edge functions and middleware.
Method | Type Signature | Description |
---|---|---|
clientId
clientId: string
The organization ID/API key of a Ninetailed account.
environment
environment?: string
The environment key of a Ninetailed account. Typically either main
or development
, depending on the environment in which you have configured your content source connections. your you have configured Defaults to main
if unsupplied.
url
url?: string
Specify the base URL of the Experience API. This should usually be left unspecified. Defaults to the production Experience API base URL of https://experience.ninetailed.co when unspecified.
fetchImpl
fetchImpl?: FetchImpl
A NinetailedApiClient
can be used in different JavaScript runtimes including the browser, Node.js, and edge workers. However, a implementation of the fetch()
method available in the browser may not be available within all of those run times. This option allows you to provide a fetch()
implementation of your own should the runtime not expose one automatically.
upsertProfile
upsertProfile({ profileId, events }: { profileId?: string; events: Event[];} options?: RequestOptions): Promise<ProfileWithSelectedVariants>
If a profile ID is not supplied, calls updateProfile
. Otherwise, calls createProfile
.
createProfile
createProfile({ events }: { events: Event[];}, options?: RequestOptions): Promise<ProfileWithSelectedVariants>
Creates a profile with the specified profile ID. Interfaces with the create profile endpoint.
updateProfile
createProfile({ profileId, events }: { profileId: string, events: Event[];}, options?: RequestOptions): Promise<ProfileWithSelectedVariants>
Creates a profile at the specified profile ID. Interfaces with the update profile endpoint.
getProfile
getProfile(id: string, options?: Omit<RequestOptions, 'preflight' | 'plaintext'> ): Promise<ProfileWithSelectedVariants>
Get the profile with the specified profile ID. Interfaces with the get profile endpoint.
upsertManyProfiles
upsertManyProfiles({events} : {events: Event[], options?: { timeout?: number | undefined; enabledFeatures?: Feature[] | undefined; }): Promise<ProfileWithSelectedVariants[]>
Supply an array of events to update many profiles at once. Responds with the representation of each upserted profile. Interfaces with the batch upsert profile endpoint.