# connect()

Canonical URL: https://developers.iagenify.com/sdk/ref/storage/connect/
Language: en

`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.

Status: POST /api/storage/connect · 201 · not tested at connection

## Parameters {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `type` | string | required | `database` or `object`. |
| `provider` | string | required | Which one — see the pairs below. |
| `label` | string |  | The display name. Derived from the provider by default. |
| `endpoint.credentials` | object|string |  | **Where the secret goes.** Encrypted at rest. |
| `endpoint.options` | object |  | Anything else the provider needs. |

## The pairs that are accepted {#pairs}

| type | provider | Is |
| --- | --- | --- |
| database | mongodb | A MongoDB database. |
| database | d1 | A Cloudflare D1 database. |
| object | r2 | A Cloudflare R2 bucket. |
| object | s3 | An S3 bucket. |

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

## Put the secret in credentials {#credentials}

```javascript
const mongo = await 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 {#untested}

`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()`.

```javascript
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}`);
}
```

## Returns {#returns}

```javascript
mongo.id;
mongo.label;       // "Prod"
mongo.type;        // "database"
mongo.provider;    // "mongodb"
mongo.managedBy;   // who operates it
mongo.isDefault;
mongo.createdAt;
```

## Give it the narrowest credential that works {#scope}

- 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 {#errors}

| Code | Status | When |
| --- | --- | --- |
| — (thrown locally) | — | type or provider missing. |
| invalid_external_type_provider_combination | 400 | That pair is not one of the four. |
| credentials_encryption_failed | 500 | The secret could not be encrypted — nothing was stored. |

- [run()](/sdk/ref/connected-storage/run): Reading and writing, whatever is behind it.
- [MongoDB](/sdk/ref/providers/mongodb): What this provider accepts, and its own traps.

```python
mongo = api.storage.connect(
    label="Prod",
    type="database",
    provider="mongodb",
    endpoint={"credentials": {"uri": "mongodb+srv://user:password@host/mydb"}},
)
```

```python
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
```

```python
mongo.id
mongo.label        # "Prod"
mongo.type         # "database"
mongo.provider     # "mongodb"
mongo.managed_by   # who operates it
mongo.is_default
mongo.created_at
```
