Developer Docs
SDK: Formbricks API
Overview
The Formbricks Client API Wrapper is a lightweight package designed to simplify the integration of Formbricks API endpoints into your JavaScript (JS) or TypeScript (TS) projects. With this wrapper, you can easily interact with Formbricks API endpoints without the need for complex setup or manual HTTP requests.
Install
npm
npm install @formbricks/api
Methods
Initialize Formbricks
Initialize the Formbricks API Client for backend developers to interact with Formbricks API endpoints:
Initialize Formbricks API
import { FormbricksAPI } from "@formbricks/api";
const api = new FormbricksAPI({
apiHost: `https://app.formbricks.com`, // If you have self-hosted Formbricks, change this to your self hosted instance's URL
environmentId: "<environment-id>", // Replace this with your Formbricks environment ID
});
The API client is now ready to be used across your project. It can be used to interact with the following models:
Displays
- Create Display
Crate Display
await api.client.display.create({
surveyId: "<your-survey-id>", // required
userId: "<your-user-id>", // optional
responseId: "<your-response-id>", // optional
});
Responses
- Create Response
Create Response
await api.client.response.create({
surveyId: "<your-survey-id>", // required
finished: boolean, // required
data: {
// required
questionId: "<answer-to-this-question-in-string>",
anotherQuestionId: 123, // answer to this question in number
yetAnotherQuestionId: ["option1", "option2"], // answer to this question in array,
},
userId: "<your-user-id>",
singleUseId: "<your-single-use-id>",
ttc: {
questionId: 123,
},
meta: {
source: "<your-source>",
url: "<your-url>",
userAgent: {
browser: "<your-browser>",
device: "<your-device>",
os: "<your-os>",
},
country: "<your-country>",
},
});
- Update Response
Update Response
await api.client.response.update({
responseId: "<your-response-id>", // required
finished: boolean, // required
data: {
// required
questionId: "<answer-to-this-question-in-string>",
anotherQuestionId: 123, // answer to this question in number
yetAnotherQuestionId: ["option1", "option2"], // answer to this question in array,
},
ttc: {
// required
questionId: 123,
},
});
Attribute
- Update Attribute
Update Attribute
await api.client.attribute.update({
userId: "<your-user-id>", // required
attributes: {
key1: "value1",
key2: "value2",
}, // required
});
People
- Create Person
Create Person
await api.client.people.create({
userId: "<your-user-id>", // required
});
Storage
- Upload File:
Upload File
await api.client.storage.uploadFile(
file: File, // required (of interface File of the browser's File API)
{
allowedFileTypes: ["file-type-allowed", "for-example", "image/jpeg"],
surveyId: "<your-survey-id>",
}
);
If you have any questions or need help, feel free to reach out to us in Github Discussions