AltScore
Guides

Managed Database

Managed Database provides each tenant with a fully managed SQLite database powered by Turso/libSQL. Store custom data, run SQL queries, and integrate with your workflows.

AltScore's Managed Database feature gives you a dedicated SQLite database for your tenant. You can use it to store custom data, create reports, or extend the platform's functionality with your own tables and queries.

Key Features

  • Fully Managed: Automatic provisioning, backups, and maintenance
  • SQLite Compatible: Use standard SQL syntax
  • Secure Access: Token-based authentication with configurable expiration
  • Global Distribution: Low-latency access from anywhere via Turso's edge network

Getting Started

1. Create Your Database

To provision your managed database, make a POST request to the tenant databases endpoint:

POST /v1/tenant-databases
Authorization: Bearer YOUR_ACCESS_TOKEN

The response includes your database details and an initial access token:

{
  "database": {
    "id": "abc123",
    "hostname": "your-tenant.turso.io",
    "status": "active",
    "connectionUrl": "libsql://your-tenant.turso.io"
  },
  "token": "eyJhbGciOiJFZERTQSIsInR5cCI6IkpXVCJ9..."
}

2. Generate Access Tokens

Access tokens are short-lived credentials for connecting to your database. Generate a new token when needed:

POST /v1/tenant-databases/tokens
Authorization: Bearer YOUR_ACCESS_TOKEN
Content-Type: application/json
 
{
  "expiration": "1h",
  "authorization": "full-access"
}

Response:

{
  "token": "eyJhbGciOiJFZERTQSIsInR5cCI6IkpXVCJ9...",
  "expiresAt": "2024-01-15T12:00:00Z"
}

Available options:

  • expiration: Token lifetime (e.g., 30m, 1h, 24h). Maximum is 24 hours.
  • authorization: full-access (read/write) or read-only

3. Connect to Your Database

Use the connection URL and token to connect to your database.

Using the HTTP API

curl -X POST "https://YOUR_DATABASE.turso.io/v2/pipeline" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "requests": [
      {"type": "execute", "stmt": {"sql": "SELECT * FROM my_table"}}
    ]
  }'

Using libSQL SDK (TypeScript)

import { createClient } from '@libsql/client'
 
const client = createClient({
  url: 'libsql://YOUR_DATABASE.turso.io',
  authToken: 'YOUR_TOKEN'
})
 
const result = await client.execute('SELECT * FROM my_table')
console.log(result.rows)

Using libSQL SDK (Python)

import libsql_experimental as libsql
 
conn = libsql.connect(
    "libsql://YOUR_DATABASE.turso.io",
    auth_token="YOUR_TOKEN"
)
 
result = conn.execute("SELECT * FROM my_table")
for row in result:
    print(row)

Managing Your Database

Check Database Status

GET /v1/tenant-databases
Authorization: Bearer YOUR_ACCESS_TOKEN

Response:

{
  "id": "abc123",
  "hostname": "your-tenant.turso.io",
  "status": "active",
  "connectionUrl": "libsql://your-tenant.turso.io",
  "tokenExpiresAt": "2024-01-15T12:00:00Z"
}

Suspend Database

If you need to temporarily disable access to your database:

POST /v1/tenant-databases/suspend
Authorization: Bearer YOUR_ACCESS_TOKEN
Content-Type: application/json
 
{
  "reason": "Maintenance window"
}

Activate Database

Resume a suspended database:

POST /v1/tenant-databases/activate
Authorization: Bearer YOUR_ACCESS_TOKEN

Best Practices

  1. Token Management: Generate tokens with the shortest practical expiration time
  2. Use Read-Only Tokens: For reporting or analytics, use read-only authorization
  3. Connection Pooling: Reuse database connections when possible
  4. Error Handling: Handle token expiration gracefully and regenerate when needed

Limitations

  • Each tenant is limited to one managed database
  • Token expiration maximum is 24 hours
  • Database size limits may apply based on your plan

Next Steps