Node.js
Learn how to integrate Kobble using our Node.js SDK.
Getting started
Installation
Download the Node.js SDK using your favorite package manager.
Get your secret key
You can obtain your secret key from your dashboard.
Test your integration
Authentication
These methods allow you to verify that incoming requests on your server come from authenticated and legitimate users.
You can pass either ID Token or Access Token to your backend. To get these tokens, you can use the frontend SDKs.
The ID token is a JWT token that contains the user’s identity information.
The Access Token is a JWT token that only contains the user id and project id.
Verify ID Token
You can verify ID tokens obtained using Kobble frontend SDKs as follows:
Verify Access Token
You can verify access tokens obtained using Kobble frontend SDKs as follows:
Users
Create a new user
You can use the create
method to create a new user with the specified details.
Parameters
The email address of the user.
The name of the user.
The phone number of the user.
Additional metadata about the user, as key-value pairs.
A flag indicating whether the email should be marked as verified.
A flag indicating whether the phone number should be marked as verified.
Returns
This function returns a promise that resolves to the newly created user object.
Example Usage
Create a login link
You can use the createLoginLink
method to generate a magic link so the user can log in to your Kobble Customer Portal.
This method can be useful when you’re building a WhatsApp bot, for example, and you want to send a login link to the user.
Parameters
The unique identifier of the user for whom the login link is being created.
Returns
This function returns a promise that resolves to an object containing the following properties:
url
(string
): The generated login URL.expiresAt
(Date
): The expiration date and time of the login link.
Example Usage
List users
This method allows you to list all users in your project. For performance reasons, the method will return a maximum of 100 users.
Get user by ID
The getById
function is an asynchronous method used to retrieve a user by their unique identifier. This function accepts two parameters: id
and an optional options
object.
Parameters
id
(string
): The unique identifier of the user to be retrieved.options
(object
, optional): An optional object that can contain the following properties:includeMetadata
(boolean
, optional): A flag indicating whether to include metadata in the retrieved user information. Default isfalse
.
Returns
Promise<User>
: A promise that resolves to aUser
object containing the user’s information.
Example Usage
Get user by email
You can use the getByEmail
method to retrieve a user’s details using their email address.
Parameters
The email address of the user to be retrieved.
An optional object to specify additional options for retrieving the user. The properties include:
includeMetadata
(boolean
, optional): A flag indicating whether to include the user’s metadata in the response.
Returns
This function returns a promise that resolves to the user object corresponding to the provided email address.
Example Usage
Find users by Metadata
The findByMetadata
function is an asynchronous method used to retrieve users based on specific metadata criteria. This function accepts two parameters: metadata
and an optional options
object.
Parameters
metadata
(Record<string, string>
): An object containing the metadata key-value pairs to filter users. The keys and values are both strings.options
(object
, optional): An optional object that can contain the following properties:page
(number
, optional): The page number for pagination. Default is1
.limit
(number
, optional): The number of users to retrieve per page. Default is50
.
Returns
Promise<Paginated<User>>
: A promise that resolves to a paginated object containing the users that match the metadata criteria.
Example Usage
This is how you can find all your users having a location set to ‘New York’ and a preference set to ‘dark’:
Limitations
Update user metadata
You can use the updateMetadata
method to store data into a user.
Unlike patchMetadata
, which atomically updates the metadata, this method replaces the entire payload stored in the user’s metadata field with the new data provided.
Parameters
userId
(string
): The unique identifier of the user whose metadata needs to be updated.metadata
(Record<string, any>
): An object containing the metadata key-value pairs to be updated for the user. The keys are strings, and the values can be of any type.
Returns
void
: This function does not return a value. It performs the update operation on the specified user’s metadata.
Example Usage
Patch user metadata
You can use the patchMetadata
method to atomically update the metadata payload of the user.
Unlike updateMetadata
, which replaces the whole metadata payload, this method replaces only the provided fields.
Parameters
userId
(string
): The unique identifier of the user whose metadata needs to be updated.metadata
(Record<string, any>
): An object containing the metadata key-value pairs to be updated for the user. The keys are strings, and the values can be of any type.
Returns
void
: This function does not return a value. It performs the update operation on the specified user’s metadata.
Example Usage
Get user product
This method will fetch the product currently attached to the user. It may be a free product or a paid product.
Quotas
List user quotas
This method will fetch all the quotas usage of the product attached to the user. It won’t fetch quotas not attached to the product of the user.
Check Remaining Quotas
The hasRemainingQuota
method checks if a user has remaining credit for the specified quota(s).
This is useful to prevent users from exceeding their assigned limits in your application.
You can pass an array of quota names or a single quota name as a string.
Increment Quota Usage
The incrementQuotaUsage
method allows you to increment the usage of a quota for a user.
This is useful when you want to track the usage of a specific quota in your application.
By default the increment is 1, but you can specify a different increment value.
Decrement Quota Usage
In rare scenarios, you may need to decrement the usage of a quota for a user.
Set Quota Usage
The setQuotaUsage
method allows you to set the usage of a quota for a user.
This is only useful when you need to set the usage of a quota to a specific value that may be lower or higher than the current usage.
Permissions
List user permissions
This method will fetch all the permissions of the product attached to the user. As for quotas, it won’t fetch permissions not attached to the product of the user.
Check Permissions
The hasPermission
method allows you to verify if a user has all the specified permissions.
This is particularly useful for authorization checks within your application.
You can pass an array of permission or a single permission string.
Webhooks
The Admin SDK provides an utility function that verifies the signature of incoming webhook events and returns a typed event object.
Construct Event
You can use the webhooks.constructEvent()
method to handle incoming webhook events from Kobble.
Here’s an example with express: