A TypeScript wrapper around the Bubble Data API.
First, you will need to provide your app
and apiKey
from Bubble.
Make sure to follow Bubble's documentation for "Activating the API" and "Setting up the GET/DATA API".
import BubbleSDK from "bubble-sdk";
BubbleSDK.init({
app: "your-app-name",
apiKey: "your-bubble-api-key",
});
Then you can create a class for each Data type in Bubble. You must define the type
. This type
is the name of the type as it appears in the URL when making requests to Bubble.
class User extends BubbleSDK.DataType {
// The name of the type in Bubble.
type = "user";
// Define your custom fields with their types.
email: string;
first_name: string;
}
To fetch by ID:
const user: User = await User.getByID("123");
To search:
const res: SearchResponse<User> = await User.search({
constraints: [
{ key: "first_name", constraint_type: "contains", values: ["asdf"] },
],
sort: {
sort_field: "Created Date",
descending: true,
},
});
To get one item by searching:
const user: User | null = await User.getFirst(/** Accepts search options */);
To get all data matching search: Use with caution as this will make unlimited API requests to Bubble in order to page through all results
const users: User[] = await User.getAll(/** Accepts search options */);
Creating a new object returns the ID of the created object.
const userID: string = await User.create({ email: "..." });
Update an object by calling .save()
const user: User = await User.getByID("123");
user.email = "new@email.com";
await user.save();