|
1 |
| -# Eppo Server-Side SDK for Node.js |
| 1 | +# Eppo Node SDK |
2 | 2 |
|
3 |
| -[](https://www.npmjs.com/package/@eppo/node-server-sdk) |
4 |
| -[](https://eppo-exp.github.io/node-server-sdk/node-server-sdk.html) |
| 3 | +[](https://github.com/Eppo-exp/node-server-sdk/actions/workflows/lint-test-sdk.yml) |
5 | 4 |
|
6 |
| -## Getting Started |
| 5 | +[Eppo](https://www.geteppo.com/) is a modular flagging and experimentation analysis tool. Eppo's Node SDK is built to make assignments in multi-user server side contexts. Before proceeding you'll need an Eppo account. |
7 | 6 |
|
8 |
| -Refer to our [SDK documentation](https://docs.geteppo.com/feature-flags/sdks/node) for how to install and use the SDK. |
| 7 | +## Features |
| 8 | + |
| 9 | +- Feature gates |
| 10 | +- Kill switches |
| 11 | +- Progressive rollouts |
| 12 | +- A/B/n experiments |
| 13 | +- Mutually exclusive experiments (Layers) |
| 14 | +- Dynamic configuration |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +```shell |
| 19 | +npm install @eppo/node-server-sdk |
| 20 | +``` |
| 21 | + |
| 22 | +## Quick start |
| 23 | + |
| 24 | +Begin by initializing a singleton instance of Eppo's client. Once initialized, the client can be used to make assignments anywhere in your app. |
| 25 | + |
| 26 | +#### Initialize once |
| 27 | + |
| 28 | +```javascript |
| 29 | +import { init } from "@eppo/node-server-sdk"; |
| 30 | + |
| 31 | +await init({ apiKey: "<SDK-KEY-FROM-DASHBOARD>" }); |
| 32 | +``` |
| 33 | + |
| 34 | + |
| 35 | +#### Assign anywhere |
| 36 | + |
| 37 | +```javascript |
| 38 | +import * as EppoSdk from "@eppo/node-server-sdk"; |
| 39 | + |
| 40 | +const eppoClient = EppoSdk.getInstance(); |
| 41 | + |
| 42 | +// Hypothetical user. |
| 43 | +const user = getCurrentUser(); |
| 44 | + |
| 45 | +const variation = eppoClient.getStringAssignment( |
| 46 | + 'new-user-onboarding', |
| 47 | + user.id, |
| 48 | + { country: user.country }, |
| 49 | + 'control' |
| 50 | +); |
| 51 | +``` |
| 52 | + |
| 53 | +## Assignment functions |
| 54 | + |
| 55 | +Every Eppo flag has a return type that is set once on creation in the dashboard. Once a flag is created, assignments in code should be made using the corresponding typed function: |
| 56 | + |
| 57 | +```javascript |
| 58 | +getBoolAssignment(...) |
| 59 | +getNumericAssignment(...) |
| 60 | +getIntegerAssignment(...) |
| 61 | +getStringAssignment(...) |
| 62 | +getJSONAssignment(...) |
| 63 | +``` |
| 64 | + |
| 65 | +Each function has the same signature, but returns the type in the function name. For booleans use `getBooleanAssignment`, which has the following signature: |
| 66 | + |
| 67 | +```javascript |
| 68 | +getBoolAssignment: ( |
| 69 | + flagKey: string, |
| 70 | + subjectKey: string, |
| 71 | + subjectAttributes: Record<string, any>, |
| 72 | + defaultValue: boolean, |
| 73 | +) => boolean |
| 74 | + ``` |
| 75 | + |
| 76 | +## Initialization options |
| 77 | + |
| 78 | +The `init` function accepts the following optional configuration arguments. |
| 79 | + |
| 80 | +| Option | Type | Description | Default | |
| 81 | +| ------ | ----- | ----- | ----- | |
| 82 | +| **`assignmentLogger`** | [IAssignmentLogger](https://github.com/Eppo-exp/js-client-sdk-common/blob/75c2ea1d91101d579138d07d46fca4c6ea4aafaf/src/assignment-logger.ts#L55-L62) | A callback that sends each assignment to your data warehouse. Required only for experiment analysis. See [example](#assignment-logger) below. | `null` | |
| 83 | +| **`requestTimeoutMs`** | number | Timeout in milliseconds for HTTPS requests for the experiment configurations. | `5000` | |
| 84 | +| **`numInitialRequestRetries`** | number | Number of _additional_ times the initial configurations request will be attempted if it fails. This is the request typically synchronously waited (via `await`) for completion. A small wait will be done between requests. | `1` | |
| 85 | +| **`pollAfterFailedInitialization`** | boolean | Poll for new configurations even if the initial configurations request failed. | `false` | |
| 86 | +| **`throwOnFailedInitialization`** | boolean | Throw an error (reject the promise) if unable to fetch initial configurations during initialization. | `true` | |
| 87 | +| **`numPollRequestRetries`** | number | If polling for updated configurations after initialization, the number of additional times a request will be attempted before giving up. Subsequent attempts are done using an exponential backoff. | `7` | |
| 88 | + |
| 89 | +## Assignment logger |
| 90 | + |
| 91 | +To use the Eppo SDK for experiments that require analysis, pass in a callback logging function to the `init` function on SDK initialization. The SDK invokes the callback to capture assignment data whenever a variation is assigned. The assignment data is needed in the warehouse to perform analysis. |
| 92 | + |
| 93 | +The code below illustrates an example implementation of a logging callback using [Segment](https://segment.com/), but you can use any system you'd like. The only requirement is that the SDK receives a `logAssignment` callback function. Here we define an implementation of the Eppo `IAssignmentLogger` interface containing a single function named `logAssignment`: |
| 94 | + |
| 95 | +```javascript |
| 96 | +import { IAssignmentLogger } from "@eppo/node-server-sdk"; |
| 97 | +import { AnalyticsBrowser } from "@segment/analytics-next"; |
| 98 | + |
| 99 | +// Connect to Segment (or your own event-tracking system) |
| 100 | +const analytics = AnalyticsBrowser.load({ writeKey: "<SEGMENT_WRITE_KEY>" }); |
| 101 | + |
| 102 | +const assignmentLogger: IAssignmentLogger = { |
| 103 | + logAssignment(assignment) { |
| 104 | + analytics.track({ |
| 105 | + userId: assignment.subject, |
| 106 | + event: "Eppo Assignment", |
| 107 | + type: "track", |
| 108 | + properties: { ...assignment }, |
| 109 | + }); |
| 110 | + }, |
| 111 | +}; |
| 112 | +``` |
| 113 | + |
| 114 | +## Philosophy |
| 115 | + |
| 116 | +Eppo's SDKs are built for simplicity, speed and reliability. Flag configurations are compressed and distributed over a global CDN (Fastly), typically reaching your servers in under 15ms. Server SDKs continue polling Eppo’s API at 30-second intervals. Configurations are then cached locally, ensuring that each assignment is made instantly. Evaluation logic within each SDK consists of a few lines of simple numeric and string comparisons. The typed functions listed above are all developers need to understand, abstracting away the complexity of the Eppo's underlying (and expanding) feature set. |
0 commit comments