Route a database connection with a user attribute
Add a user attribute, select it in a database connection field, and send its value in the embed token.
Target Audience: Makers & Developers
Goal
Send each viewer's queries to their own data source through one connection. The steps use the Database field. Other fields that change which data is read work the same way.
Prerequisites
- A connection field that accepts a variable and changes which data is read. The Database field does not redirect queries on MySQL.
- Sources that expose the same schemas, tables and columns.
- A backend that generates your embed tokens.
Steps
1. Add the user attribute
- Go to Users > User model.
- Under Token Attributes, click Add attribute.
- Enter a Name, for example
db_name, and select the Text type. - Click Set default value and type the value as is, without quotes. Use a source with no real tenant data.
2. Select the variable in the connection
- Add or edit the database connection.
- Next to Database, click Variable (
{}icon). - Select
db_name.
3. Test and save
Click Test Connection, then save. The test uses the default value, so it does not check the other sources.
4. Send the attribute in the embed token
Call this endpoint from your server only.
POST https://toucanai.cloud/embed/generate-token
x-api-key: <YOUR_API_KEY>
Content-Type: application/json
{
"user": {
"distinctId": "user-123",
"role": "explorer",
"attributes": { "db_name": "<VIEWER_DATABASE>" }
}
}If your user model has other custom attributes, include them too.
5. Check the routing
Open a dashboard with tokens for two sources that hold different data. Each token must show only its own data, including filter values and AI answers.