docs/docs/00300-resources/00200-reference/00200-http-api/00300-database.md
/v1/databaseThe HTTP endpoints in /v1/database allow clients to interact with Spacetime databases in a variety of ways, including retrieving information, creating and deleting databases, invoking reducers and evaluating SQL queries. These APIs are intended primarily for management, debugging and interactive developer use, and have not been optimized for performance to the same extent as the WebSocket API used by the SpacetimeDB client SDKs.
POST /v1/databasePublish a new database with no name.
Accessible through the CLI as spacetime publish.
| Name | Value |
|---|---|
Authorization | A Spacetime token as Bearer auth. |
If no Authorization header is provided, a new anonymous identity will be created and will own the new database. This is generally not what you want.
A WebAssembly module in the binary format.
If the database was successfully published, returns JSON in the form:
{ "Success": {
"database_identity": string,
"op": "created" | "updated"
} }
PUT /v1/database/:name_or_identityPublish to a database with the specified name or identity. If the name does not exist, creates a new database.
Accessible through the CLI as spacetime publish.
| Name | Value |
|---|---|
clear | A boolean; whether to clear any existing data when updating an existing database. |
| Name | Value |
|---|---|
Authorization | A Spacetime token as Bearer auth. |
If no Authorization header is provided, a new anonymous identity will be created. When updating an existing database, the token must correspond to the database's owner, or the request will be rejected.
A WebAssembly module in the binary format.
If the database was successfully published, returns JSON in the form:
{ "Success": {
"domain": null | string,
"database_identity": string,
"op": "created" | "updated"
} }
If a database with the given name exists, but the identity provided in the Authorization header does not have permission to edit it, returns 401 UNAUTHORIZED along with JSON in the form:
{ "PermissionDenied": {
"name": string
} }
GET /v1/database/:name_or_identityGet a database's identity, owner identity, host type, number of replicas and a hash of its WASM module.
Returns JSON in the form:
{
"database_identity": string,
"owner_identity": string,
"host_type": "wasm",
"initial_program": string
}
| Field | Type | Meaning |
|---|---|---|
"database_identity" | String | The Spacetime identity of the database. |
"owner_identity" | String | The Spacetime identity of the database's owner. |
"host_type" | String | The module host type; currently always "wasm". |
"initial_program" | String | Hash of the WASM module with which the database was initialized. |
DELETE /v1/database/:name_or_identityDelete a database.
Accessible through the CLI as spacetime delete <identity>.
| Name | Value |
|---|---|
Authorization | A Spacetime token as Bearer auth. |
Deleting a database requires ownership. If no Authorization header is provided, the request will be treated as anonymous and will be rejected.
GET /v1/database/:name_or_identity/namesGet the names this database can be identified by.
Returns JSON in the form:
{ "names": array<string> }
where <names> is a JSON array of strings, each of which is a name which refers to the database.
POST /v1/database/:name_or_identity/namesAdd a new name for this database.
| Name | Value |
|---|---|
Authorization | A Spacetime token as Bearer auth. |
If no Authorization header is provided, the request will be treated as anonymous.
Takes as the request body a string containing the new name of the database.
If the name was successfully set, returns JSON in the form:
{ "Success": {
"domain": string,
"database_result": string
} }
If the new name already exists but the identity provided in the Authorization header does not have permission to edit it, returns JSON in the form:
{ "PermissionDenied": {
"domain": string
} }
PUT /v1/database/:name_or_identity/namesSet the list of names for this database.
| Name | Value |
|---|---|
Authorization | A Spacetime token as Bearer auth. |
Setting names requires ownership of the database. If no Authorization header is provided, the request will be treated as anonymous and will be rejected.
Takes as the request body a list of names, as a JSON array of strings.
If the name was successfully set, returns JSON in the form:
{ "Success": null }
If any of the new names already exist but the identity provided in the Authorization header does not have permission to edit it, returns 401 UNAUTHORIZED along with JSON in the form:
{ "PermissionDenied": null }
GET /v1/database/:name_or_identity/identityGet the identity of a database.
Returns a hex string of the specified database's identity.
GET /v1/database/:name_or_identity/subscribeBegin a WebSocket connection with a database.
For more information about WebSocket headers, see RFC 6455.
| Name | Value |
|---|---|
Sec-WebSocket-Protocol | v1.bsatn.spacetimedb or v1.json.spacetimedb |
Connection | Upgrade |
Upgrade | websocket |
Sec-WebSocket-Version | 13 |
Sec-WebSocket-Key | A 16-byte value, generated randomly by the client, encoded as Base64. |
The SpacetimeDB binary WebSocket protocol, v1.bsatn.spacetimedb, encodes messages as well as reducer and row data using BSATN.
Its messages are defined here.
The SpacetimeDB text WebSocket protocol, v1.json.spacetimedb, encodes messages according to the SATS-JSON format.
| Name | Value |
|---|---|
Authorization | A Spacetime token as Bearer auth. |
POST /v1/database/:name_or_identity/call/:reducerInvoke a reducer in a database.
| Name | Value |
|---|---|
:reducer | The name of the reducer OR procedure. |
| Name | Value |
|---|---|
Authorization | A Spacetime token as Bearer auth. |
If no Authorization header is provided, the request will be treated as anonymous. The caller's identity is passed to the reducer via its ReducerContext, and the module may accept or reject the call based on that identity.
A JSON array of arguments to the reducer.
GET /v1/database/:name_or_identity/schemaGet a schema for a database.
Accessible through the CLI as spacetime describe <name_or_identity>.
| Name | Value |
|---|---|
version | The version of RawModuleDef to return, e.g. 9. |
| Name | Value |
|---|---|
Authorization | A Spacetime token as Bearer auth. |
No authorization is required to fetch a database's schema. If an Authorization header is provided, the response will include spacetime-identity and spacetime-identity-token headers echoing the caller's identity. If omitted, a new anonymous identity will be allocated for this purpose.
Returns a RawModuleDef in JSON form.
{
"typespace": {
"types": [
{
"Product": {
"elements": [
{
"name": {
"some": "name"
},
"algebraic_type": {
"String": []
}
}
]
}
}
]
},
"tables": [
{
"name": "person",
"product_type_ref": 0,
"primary_key": [],
"indexes": [],
"constraints": [],
"sequences": [],
"schedule": {
"none": []
},
"table_type": {
"User": []
},
"table_access": {
"Private": []
}
}
],
"reducers": [
{
"name": "add",
"params": {
"elements": [
{
"name": {
"some": "name"
},
"algebraic_type": {
"String": []
}
}
]
},
"lifecycle": {
"none": []
}
},
{
"name": "identity_connected",
"params": {
"elements": []
},
"lifecycle": {
"some": {
"OnConnect": []
}
}
},
{
"name": "identity_disconnected",
"params": {
"elements": []
},
"lifecycle": {
"some": {
"OnDisconnect": []
}
}
},
{
"name": "init",
"params": {
"elements": []
},
"lifecycle": {
"some": {
"Init": []
}
}
},
{
"name": "say_hello",
"params": {
"elements": []
},
"lifecycle": {
"none": []
}
}
],
"types": [
{
"name": {
"scope": [],
"name": "Person"
},
"ty": 0,
"custom_ordering": true
}
],
"misc_exports": [],
"row_level_security": []
}
GET /v1/database/:name_or_identity/logsRetrieve logs from a database.
Accessible through the CLI as spacetime logs <name_or_identity>.
| Name | Value |
|---|---|
num_lines | Number of most-recent log lines to retrieve. |
follow | A boolean; whether to continue receiving new logs via a stream. |
| Name | Value |
|---|---|
Authorization | A Spacetime token as Bearer auth. |
Viewing logs requires ownership of the database. If no Authorization header is provided, the request will be treated as anonymous and will be rejected.
Text, or streaming text if follow is supplied, containing log lines.
POST /v1/database/:name_or_identity/sqlRun a SQL query against a database.
Accessible through the CLI as spacetime sql <name_or_identity> <query>.
| Name | Value |
|---|---|
Authorization | A Spacetime token as Bearer auth. |
If no Authorization header is provided, the request will be treated as anonymous and will only have access to public tables. The caller's identity is used to enforce row-level security policies.
SQL queries, separated by ;.
Returns a JSON array of statement results, each of which takes the form:
{
"schema": ProductType,
"rows": array
}
The schema will be a JSON-encoded ProductType describing the type of the returned rows.
The rows will be an array of JSON-encoded ProductValues, each of which conforms to the schema.
ANY /v1/database/:name_or_identity/route/{*path}Access routes defined by a database using HTTP handlers.