Connecting an application
The connection details a database has, where to find them, and which credential goes with each.
Your application connects to its database's cluster, not to the dashboard. A database's connection details tell it where the cluster is and what the database is called there.
Where to find them
Connection details exist once a database is ready. They are shown on the database's Overview under Connection, filled into the samples of its Connect and API pages, and returned by the management API as the database's connection field.
Before the cluster has confirmed the database, there are no connection details and connection is null.
The connection details
| Name | Type | Description |
|---|---|---|
clusterUrl * | string | The address of the cluster the database runs on. |
databaseId * | string | The ID the cluster knows the database by. It is not the database's management API ID. |
apiUrl * | string | The database's HTTP API, for documents: <clusterUrl>/v1/databases/<databaseId>. |
adminUrl * | string | The database's administration, for its collections: <clusterUrl>/v1/admin/databases/<databaseId>. |
websocketUrl * | string | Where realtime sessions open: the cluster's address with the ws or wss scheme, at /v1/sessions. |
{
"clusterUrl": "https://<cluster-host>",
"databaseId": "<cluster-database-id>",
"apiUrl": "https://<cluster-host>/v1/databases/<cluster-database-id>",
"adminUrl": "https://<cluster-host>/v1/admin/databases/<cluster-database-id>",
"websocketUrl": "wss://<cluster-host>/v1/sessions"
}The management API identifies a database by an ID that starts with db_. The cluster identifies
it by connection.databaseId. Use the first in management API paths and the second, through the
addresses above, in everything your application sends to the cluster.
Your backend
Your backend authenticates to apiUrl and adminUrl with a database key, sent as a bearer token:
Authorization: Bearer mk_…The dashboard's samples read the addresses and the key from these environment variables:
MIMIC_DATABASE_API_URL=https://<cluster-host>/v1/databases/<cluster-database-id>
MIMIC_DATABASE_ADMIN_URL=https://<cluster-host>/v1/admin/databases/<cluster-database-id>
MIMIC_DATABASE_KEY=mk_…The database's Connect and API pages give requests with your addresses filled in.
Your application's users
Browsers and mobile apps must never hold a database key. They open realtime sessions at websocketUrl with a document token: a short-lived credential for one document. There are two ways to get one:
- Your backend asks the database's HTTP API for a token, using a database key. A token that allows writing needs a key with the write permission.
- Your backend signs tokens itself with a private key whose public half you registered as a token key.
When the connection details go away
| State | Connection details |
|---|---|
provisioning, failed | None |
ready, suspended | Present |
pending_deletion | Present if the database was ready or suspended before, but the cluster refuses connections |
deleting | None |
The addresses of a database do not change when it is renamed, suspended, resumed or restored after a deletion.
A database restored from a backup is a new database, with its own IDs and addresses. See backups.