API Endpoints

advanced8 min read

API Endpoints

Reference for the AI for Database API. All paths are relative to https://app.aifordatabase.com/api/v1.

For exact request/response schemas, use the live OpenAPI spec or the interactive reference. The spec is maintained alongside the route handlers and is the contract for public v1 integrations.

Connections

MethodPathDescription
GET / POST/connectionsList / create connections (create is admin-only)
GET / PATCH / DELETE/connections/:idRead / update / remove a connection
POST/connections/:id/testTest connectivity
GET / POST/connections/:id/schemaRead cached schema / introspect now
POST/connections/:id/queryRun SQL directly against the connection
GET / POST/connections/:id/annotationsList / add schema annotations
POST/connections/:id/annotations/autoAI-generate annotations

Create a connectiontype must be one of POSTGRES, MYSQL, MARIADB, MSSQL, MONGODB, SQLITE:

json
{
  "name": "Production DB",
  "type": "POSTGRES",
  "host": "db.example.com",
  "port": 5432,
  "database": "production",
  "username": "aifordb_reader",
  "password": "your-db-password",
  "sslEnabled": true
}

The password is write-only — it is never returned by any endpoint. (encryptedPassword and ssl are accepted as aliases for password / sslEnabled.)

Setup flow: creating a connection stores it; then call POST /connections/:id/test. On its first success, the test also introspects your tables and generates the AI schema overview automatically — create → test is the complete setup. Use POST /connections/:id/schema to re-introspect after your schema changes.

Failed tests return a structured diagnosis in the error details — what stage failed (DNS, connect, TLS, auth, schema read), whose fault it is, and concrete fix steps with your actual host/database/username filled in.

Connection health: connections are tested automatically in the background. If a connection fails 3 checks in a row, it's deactivated and the reason is saved — check lastError and lastErrorDiagnosis on GET /connections/:id any time to see why, without having to re-run a test yourself. Subscribe a webhook to connection.health.degraded to be notified the moment it happens instead of finding out from a failed query.

Chat & Conversations

MethodPathDescription
POST/chatSend a natural-language message, get AI-generated SQL and results
GET / POST/conversationsList / create conversations
GET / PATCH / DELETE/conversations/:idManage a conversation
GET/conversations/:id/messagesMessage history

Query Approval

MethodPathDescription
POST/queries/submitSubmit a query for approval
GET/queries/pendingList queries awaiting approval
GET/queries/:id/statusCheck approval status
POST/queries/:id/approve / /queries/:id/rejectApprove or reject
GET / POST/approval-rulesList / create auto-approval rules

Saved Queries & Metrics

MethodPathDescription
GET / POST/saved-queriesList / create saved queries
GET / PATCH / DELETE/saved-queries/:idManage a saved query
POST/saved-queries/:id/runExecute (with parameters)
GET / POST/metricsList / define metrics
GET/metrics/:id/valueEvaluate a metric now

Dashboards

MethodPathDescription
GET / POST/dashboardsList / create dashboards
GET / PATCH / DELETE/dashboards/:idManage a dashboard
GET / POST/dashboards/:id/widgetsList / add widgets
PATCH / DELETE/dashboards/:id/widgets/:widgetIdUpdate / remove a widget
GET/dashboards/:id/widgets/:widgetId/dataWidget's current data

Workflows

MethodPathDescription
GET / POST/workflowsList workflows / create an inactive draft
GET / PATCH / DELETE/workflows/:idRead, edit, publish/unpublish, or delete
POST/workflows/:id/previewRun only draft queries and return raw rows; never deliver or persist a workflow run
POST/workflows/:id/runRun the current draft, including actions
POST/workflows/:id/actions/:order/testSend one explicitly confirmed action test
GET/workflows/:id/runsPaginated run history
GET / POST/workflow-credentialsList metadata / create an encrypted credential
GET / PATCH / DELETE/workflow-credentials/:idRead metadata, rotate the secret, or safely delete

Workflow creation never schedules an incomplete draft. PATCH /workflows/:id can replace the draft's query steps and actions; include the current draftRevision as expectedDraftRevision so concurrent edits cannot be overwritten. Set isActive: true to validate and publish an immutable production version; set it to false to stop future scheduled runs. Manual runs use the current draft and have real external side effects.

Webhook action authentication is referenced by an encrypted, exact-host-bound credentialId. Create one through POST /workflow-credentials using the dedicated workflow_credentials scope and an admin-owned API key; the response contains metadata but never the secret. Inline authentication secrets are rejected. Preview the draft first to inspect raw eligible rows without delivery. Then call the action-test endpoint with {"confirmDelivery":true}; it sends a real request and returns the run with sanitized HTTP attempts. Run history includes the same attempt status, status code, retry timing, and bounded credential-redacted response data.

By default, every HTTP 2xx response is successful. Set successStatusCodes on a webhook action when the destination distinguishes full and partial acceptance. For example, Brevo's batch Events API uses [202]: HTTP 207 then becomes a visible failed delivery with the bounded response body instead of a false success.

Webhooks, Usage & Keys

MethodPathDescription
GET / POST/webhooksList / create signed event webhook endpoints
GET / PATCH / DELETE/webhooks/:idManage a signed event webhook
GET/webhooks/:id/deliveriesDelivery log
POST/webhooks/:id/testSend a test event
GET/usageUsage records
GET/usage/budgetPersistent balance in USD cents (fractional cents supported); no expiry or quota periods
GET / POST/keysList / create API keys
DELETE/keys/:idRevoke a key

Event webhook destinations must use public HTTPS on port 443. Private/reserved networks and unsafe redirects are blocked. Deliveries are HMAC-signed and non-2xx responses are failures; retryable responses are retried.

Error Codes

CodeHTTP StatusDescription
UNAUTHORIZED401Missing, invalid, or expired API key
FORBIDDEN403Key lacks the required scope, or admin role required
NOT_FOUND404Resource does not exist (or belongs to another org)
BAD_REQUEST400Malformed JSON body
VALIDATION_ERROR400Missing or invalid request fields
PRECONDITION_REQUIRED428Draft update omitted expectedDraftRevision
CONFIRMATION_REQUIRED428Live action test omitted explicit confirmation
ACTION_TEST_FAILED422Controlled action delivery ran but failed
PREVIEW_FAILED422Workflow queries could not be previewed because a source is unavailable
CONNECTION_FAILED422Database connection test failed
INTROSPECTION_FAILED422Schema introspection failed
RATE_LIMITED429Too many requests
INTERNAL_ERROR500Unexpected server error

Ready to try this on your own database?

Connect in minutes and ask your first question — no SQL required.

Start free

Free plan · No credit card required