api.storage

connect()

api.storage.connect({ label, type, provider, endpoint }) → Promise<ConnectedStorage>

Registers a database or a bucket of your own. The credentials are encrypted on arrival and never handed back.

POST /api/storage/connect201not tested at connection
Using Python?

Switch any code block to Python: the choice applies to every page. Python SDK guide and resource reference.

Parameters

typestringrequired

database or object.

providerstringrequired

Which one — see the pairs below.

labelstring

The display name. Derived from the provider by default.

endpoint.credentialsobject|string

Where the secret goes. Encrypted at rest.

endpoint.optionsobject

Anything else the provider needs.

The pairs that are accepted

typeproviderIs
databasemongodbA MongoDB database.
databased1A Cloudflare D1 database.
objectr2A Cloudflare R2 bucket.
objects3An S3 bucket.

Anything else is refused with invalid_external_type_provider_combination — before anything is stored.

Put the secret in credentials

const mongo = await api.storage.connect({
  label: "Prod",
  type: "database",
  provider: "mongodb",
  endpoint: { credentials: { uri: "mongodb+srv://user:password@host/mydb" } },
});
mongo = api.storage.connect(
    label="Prod",
    type="database",
    provider="mongodb",
    endpoint={"credentials": {"uri": "mongodb+srv://user:password@host/mydb"}},
)
credentials is the field that is encrypted

A connection URI usually carries the password, so it belongs there and nowhere else. Nothing about a storage hands its endpoint back afterwards — the server uses it to connect, and you are the one who supplied it.

For MongoDB, the database is read from the path of the URI — …/mydb. Leave it out and the connection lands on a default database, which will look empty and is the usual explanation for « my collections are missing ».

It is registered, not tested

connect() succeeds without opening the connection. A wrong URI, a revoked password, a firewall — none of it surfaces here; it surfaces on the first run().

const mongo = await api.storage.connect({ label, type: "database", provider: "mongodb",
  endpoint: { credentials: { uri } } });

// Prove it works now, rather than at the worst moment later
try {
  await mongo.run({ action: "collections" });
} catch (err) {
  await api.storage.remove(mongo.id);
  throw new Error(`that connection does not work: ${err.message}`);
}
from iagenify import IaGenifyError

mongo = api.storage.connect(label=label, type="database", provider="mongodb",
                            endpoint={"credentials": {"uri": uri}})

# Prove it works now, rather than at the worst moment later
try:
    mongo.run(action="collections")
except IaGenifyError as error:
    api.storage.remove(mongo.id)
    raise RuntimeError(f"that connection does not work: {error}") from error

Returns

mongo.id;
mongo.label;       // "Prod"
mongo.type;        // "database"
mongo.provider;    // "mongodb"
mongo.managedBy;   // who operates it
mongo.isDefault;
mongo.createdAt;
mongo.id
mongo.label        # "Prod"
mongo.type         # "database"
mongo.provider     # "mongodb"
mongo.managed_by   # who operates it
mongo.is_default
mongo.created_at

Give it the narrowest credential that works

  • A user that can read what you need and nothing else, rather than an administrator.
  • A separate credential per connection, so revoking one does not break the others.
  • Something you can rotate without us: to change it, connect again and remove the old one.
An agent can be given this storage, and then it reads it

Reading is one permission category and writing is another, both asked before anything happens. Decide what the credential allows as well as what the mode allows — the credential is the harder boundary.

Errors

CodeStatusWhen
— (thrown locally)—type or provider missing.
invalid_external_type_provider_combination400That pair is not one of the four.
credentials_encryption_failed500The secret could not be encrypted — nothing was stored.