Connect a database
Connect a PostgreSQL or MySQL database so the agent can look up data, such as an order's status. Test the connection, then assign its tools to the agent.
Before you start
You need an agent, its id in AGENT_ID and a database user with read-only permission.
For Supabase, use the Session pooler connection details: the direct host only has IPv6.
To connect it from the platform, see Database.
Connect the database
Send the password when creating the connection: it is stored as a secret and is not returned by reads. Also define which columns the agent must not see. (Connect a database)
curl https://api.platic.ai/v1/tools/databases \ -H "Authorization: Bearer $PLATIC_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "name": "store", "description": "Orders and customers of the online store.", "connectionString": "'$DATABASE_URL'", "hiddenColumns": ["customers.phone"] }'import { Platic } from "platic-sdk"; const platic = new Platic({ apiKey: process.env.PLATIC_API_KEY! }); const agentId = process.env.AGENT_ID!; const db = await platic.tools.databases.create({ name: "store", description: "Orders and customers of the online store.", connectionString: process.env.DATABASE_URL!, hiddenColumns: ["customers.phone"], });Connect my store's database to Platica and hide the phone column of customers.
Tool:
create_database. To keep the password out of the chat, create the connection yourself in Integrations → Functions → Database and ask it to continue from there.DATABASE_URLis the connection string, likepostgres://reader:<password>@db.acme.com:5432/store. The response has the connection'sid(keep it inDATABASE_ID) and, intools, its four tools.Test it
Test the connection to check that Platica can reach the database and see its tables. (Test a database)
curl -X POST https://api.platic.ai/v1/tools/databases/$DATABASE_ID/test \ -H "Authorization: Bearer $PLATIC_API_KEY"const test = await platic.tools.databases.test(db.id); if (!test.connected) throw new Error(`${test.error.code}: ${test.error.message}`);Test the connection to the store database and tell me which tables it sees.
Tool:
test_database.If
connectedisfalse,errorsays what to check. See Test a database.Give its tools to the agent
Each tool is connected separately, with its
id. It applies right away, without publishing the agent. (Connect a tool)curl -X PUT https://api.platic.ai/v1/agents/$AGENT_ID/tools/$TOOL_ID \ -H "Authorization: Bearer $PLATIC_API_KEY"Repeat the call with the
idof each tool intools.for (const tool of db.tools) { await platic.agents.tools.connect(agentId, tool.id); }Give the Sales agent the tools of the store database.
Tool:
connect_agent_tool, once per tool.
Check it worked
Use List the agent's tools to check that all four database tools are assigned. Ask about a known value, such as an order's status, and check the reply.
The agent only runs SELECT queries, up to 200 rows per page and within the connection's time limit. To check what it will find, run a query yourself with Query a database.
Next steps
- Test it before you publish: a scenario can require the agent to query the database.
- Databases: passwords, data access and query limits.