Authentication with Sub-accounts
This tutorial covers how to authenticate and make requests on behalf of sub-accounts (child users) within Chatbotkit. Sub-accounts are commonly used in multi-tenant architectures or partner integrations.
1. Sub-Account Authentication
You can authenticate and execute actions on behalf of sub-accounts by using either their user ID or email.
Using runAsChildUserId
const cbk = new ChatBotKit({ secret: 'your-partner-api-secret', runAsChildUserId: 'sub-account-user-id' });
javascript
Using runAsChildUserEmail
const cbk = new ChatBotKit({ secret: 'your-partner-api-secret', runAsChildUserEmail: 'subuser@example.com' });
javascript
secret
: Partner-level API key.runAsChildUserId
orrunAsChildUserEmail
: Identifies the sub-account.
2. Making Requests as a Sub-Account
Creating a Dataset as a Sub-Account
const dataset = await cbk.dataset.create({ name: 'Customer Knowledge Base', description: 'FAQ and support articles', visibility: 'private' });
javascript
Listing Datasets for a Sub-Account
const datasets = await cbk.dataset.list({ take: 10 }); console.log(datasets);
javascript
Updating a Dataset as a Sub-Account
const updatedDataset = await cbk.dataset.update('dataset-id', { description: 'Updated description' });
javascript
Deleting a Dataset as a Sub-Account
await cbk.dataset.delete('dataset-id');
javascript
3. Best Practices
- Secure API Secrets: Store API keys securely and avoid exposing them.
- Use
runAsChildUserId
for Internal Operations: Prefer IDs for better security. - Audit Requests: Maintain logs of actions performed on behalf of sub-accounts.
Conclusion
This tutorial demonstrated how to authenticate as a sub-account and manage datasets within Chatbotkit. Leveraging sub-accounts allows for efficient multi-tenant architecture management.