Appearance
Getting started
Create an account in the TerraScale dashboard, then create a database and an API key.
Fastest path: install the CLI, run ts auth login, then ts init — it creates the database, the scoped API key and a ready-to-use .env in one command.
Create an account, a database and an API key
- Open dash.terrascale.tech/register, select Create account, and enter the code sent to your email.
- Finish setup: choose a plan (or continue without a payment method), then name your first project and pick its region.
- Open Databases, select New database, enter a name starting with a lowercase letter and containing up to 63 lowercase letters, digits, underscores or hyphens (reserved names con, prn, aux, nul, com0–com9 and lpt0–lpt9 are rejected), and select Create database.
- Open API keys and select Create key. Name it, limit Database to that database, check sql:read and sql:write. Copy the key when the dashboard shows it and keep it in your secret manager.
The key has the form <key ID>_<secret>. It is a client credential, not a bearer token: the API answers 401 when you send it directly. A key may start with sk_.
Exchange the key for an access token
Use Bash, curl and jq, with shell tracing off. Every request to the API at https://api.terrascale.tech carries a short-lived access token. Get one from the token endpoint with the OAuth 2.0 client credentials grant: the client ID is the key ID after an optional sk_, and the client secret is the whole key.
bash
set +x
set -o pipefail
read -rs TERRASCALE_API_KEY && # paste the key, then press Enter
ts_token() {
key="${TERRASCALE_API_KEY#sk_}"
printf 'user = "%s:%s"\n' "${key%%_*}" "$TERRASCALE_API_KEY" |
curl -sS --fail-with-body --config - -d grant_type=client_credentials \
https://auth.terrascale.tech/terrascale/oauth/token |
jq -er '.access_token | select(type == "string" and length > 0)'
}
export TERRASCALE_ACCESS_TOKEN="$(ts_token)"Each token lasts 60 seconds (expires_in: 60) and carries the key's scopes; call ts_token again for a fresh one, as in Authorization: Bearer $(ts_token). Authentication explains the contract.
Query from your application
from terrascale import TerraScale
# Server-side: reads TERRASCALE_API_KEY and TERRASCALE_PROJECT_ID.
with TerraScale() as client:
db = client.database("app")
customers = db.query(
"SELECT email, seats FROM customers WHERE seats >= ? ORDER BY seats DESC LIMIT 25",
[10],
)
for row in customers:
print(row.get_string("email"), row.get_int64("seats"))import { createTerraBaseV1Client } from "@terrascale/sdk";
// Server-side: TERRASCALE_ACCESS_TOKEN is a 60-second token exchanged from
// your dashboard API key. Keep both out of browser bundles.
const client = createTerraBaseV1Client({
baseUrl: "https://api.terrascale.tech",
project: process.env.TERRASCALE_PROJECT_ID,
token: process.env.TERRASCALE_ACCESS_TOKEN,
});
const db = client.database("app");
// One transaction: both statements commit or neither does.
await db.batch([
"CREATE TABLE IF NOT EXISTS customers (id INTEGER PRIMARY KEY, email TEXT UNIQUE NOT NULL, seats INTEGER NOT NULL)",
{ sql: "INSERT INTO customers (email, seats) VALUES (?, ?)", args: ["team@example.com", 12] },
]);
const customers = await db.query(
"SELECT email, seats FROM customers WHERE seats >= ? ORDER BY seats DESC LIMIT 25",
[10],
);import (
"context"
"os"
tb "terrascale.tech/sdk/v1"
"terrascale.tech/sdk/tsauth"
)
// Server-side: the token source exchanges your API key.
project := os.Getenv("TERRASCALE_PROJECT_ID")
tokens, err := tsauth.NewAPIKeySource(tsauth.APIKeyConfig{
Key: os.Getenv("TERRASCALE_API_KEY"), Project: project,
})
if err != nil { panic(err) }
client, err := tb.New(tb.Config{TokenSource: tokens})
if err != nil { panic(err) }
db := client.Database(project, "app")
customers, err := db.Query(context.Background(),
"SELECT email, seats FROM customers WHERE seats >= ? ORDER BY seats DESC LIMIT 25", 10)
if err != nil { panic(err) }using TerraScale.DotNet.TerraBase;
// Server-side: the client exchanges your dashboard API key for
// short-lived access tokens. Keep the key out of client apps.
using var http = new HttpClient(TerraBaseHttp.CreateHandler());
var sql = new TerraBaseSqlClient(http, new TerraBaseSqlClientOptions { ApiKey = apiKey });
TerraBaseSqlDatabase db = sql.Database(projectId, "app");
// One transaction: both statements commit or neither does.
await db.BatchAsync([
new("CREATE TABLE IF NOT EXISTS customers (id INTEGER PRIMARY KEY, email TEXT UNIQUE NOT NULL, seats INTEGER NOT NULL)"),
new("INSERT INTO customers (email, seats) VALUES (?, ?)", ["team@example.com", 12])
], cancellationToken);
var customers = await db.QueryAsync(
"SELECT email, seats FROM customers WHERE seats >= ? ORDER BY seats DESC LIMIT 25",
[10], cancellationToken);
foreach (var row in customers)
Console.WriteLine($"{row.GetString("email")}: {row.GetInt64("seats")}");use terrascale::v1::Client;
// Server-side: the client exchanges and refreshes project tokens.
let project = std::env::var("TERRASCALE_PROJECT_ID")?;
let api_key = std::env::var("TERRASCALE_API_KEY")?;
let client = Client::for_project_api_key(&project, &api_key).build()?;
let db = client.database("app").sql();
let customers = db.query(
"SELECT email, seats FROM customers WHERE seats >= ? ORDER BY seats DESC LIMIT 25",
&[10.into()],
).await?;
for row in customers {
println!("{}: {}", row.get_str("email")?, row.get_i64("seats")?);
}Next steps
Run your first query with the SQL quickstart from the CLI, psql or HTTP. Read errors and safe retries before you build an application that depends on error handling. Dashboard usage covers the rest of the customer workflow.