This is the official Node.js SDK for Rocketflag, a feature flag and A/B testing platform. This SDK allows you to easily integrate Rocketflag into your Javascript Web Apps or Node.js applications, enabling you to control feature rollouts, perform A/B tests, and manage your features effectively.
npm install @rocketflag/node-sdkimport createRocketflagClient from "@rocketflag/node-sdk";
const rocketflag = createRocketflagClient(); // Uses default API URL and version
// Optionally you can configure which version of the API you want to use
const rocketflag = createRocketflagClient("v2", "https://your-api-domain.com");try {
const flag = await rocketflag.getFlag("IFldMzqP5jtv9wAL");
// Do something with the flag result. For example in React, you may want to set state.
setSignUpsEnabled(flag.enabled);
} catch (error) {
console.error("Something went wrong fetching the flag", error);
}const flag = await rocketflag.getFlag("IFldMzqP5jtv9wAL", {
cohort: "<cohort-identifier>",
});The SDK can throw the following errors:
APIError: This error is thrown when the API returns a non-ok response. The error object contains the status code and status text of the response.InvalidResponseError: This error is thrown when the API returns an invalid response. This can happen if the response is not valid JSON or if it doesn't match the expected format.NetworkError: This error is thrown when there is a network error, such as a failed connection.
import { APIError, InvalidResponseError, NetworkError } from "@rocketflag/node-sdk/errors";
try {
const flag = await rocketflag.getFlag("IFldMzqP5jtv9wAL");
// ...
} catch (error) {
if (error instanceof APIError) {
console.error(`API Error: ${error.status} ${error.statusText}`);
} else if (error instanceof InvalidResponseError) {
console.error(`Invalid Response Error: ${error.message}`);
} else if (error instanceof NetworkError) {
console.error(`Network Error: ${error.message}`);
} else {
console.error("An unknown error occurred", error);
}
}The response from the API on a flag will be one of three possibilities.
-
A
200with the Flag object{ "name": "The user-created flag name", "enabled": true, "id": "asklWQQZdslhfsszZWkj" } -
A
404not found status code.A 404 indicates that the flag ID you've provided in your request is not valid and cannot be found.
-
A
500internal server error.These should be extremely rare, but trying again may help. In short something has gone wrong retrieving flag data. 500's trigger internal alerts on Rocketflag, so it's likely it's already being looked into.