# Connect

> A one-time link for one app user to connect one of their apps, so what it delivers fills their memory.

`POST /v2/connections/connect`

- Auth: read and write key (project)
- Cost: free; limited by your plan's connected app users

Makes a link for the app user named in `X-Alexein-User` to connect one app. Send that user's signed-in browser to
`url`. They approve on Alexein's page, sign in to the app, and come back to `return_to`. From then on, what the
app delivers arrives in their memory: its history first, then what is new. See
[Connect your users' apps](/guides/connect-apps).

The link is a one-time credential for that one app user: it is valid for 15 minutes and can be used once. Send
it only to that user's signed-in browser, as a redirect, never by email or into a shared channel.

## Headers

- `Authorization` (string, required): `Bearer ax_…`, an API key. The key must read and write.
- `X-Alexein-User` (string, required): The app user whose app is connected, and whose memory it fills. See [App users](/concepts/app-users).
- `X-Request-Id` (string): Your id for this request, echoed on the response.

## Body

- `provider` (string, required): The app to connect: an `id` from [`providers`](/api/connections/providers), such as `google-gmail`.
- `return_to` (string, required): Where the person's browser comes back to. `https://`, or for development `http://localhost` or
  `http://127.0.0.1`, on any port. The page they approve on shows its host, so use your own domain.
- `history_days` (integer): How far back to read: `7`, `15`, `30` or `90`. Default `90`.

No other field is taken: a body that names an app user, a project or an organisation is a `422`.

## Response

- `url` (string): The link. Send the app user's browser to it.
- `expires_at` (string): When the link stops working, 15 minutes after it was made, in ISO 8601.

## The return

- `status` (string): Added to `return_to`'s query: `connected` or `error`.
- `connection_id` (string): With `connected`: the new connection's id, as [`connections`](/api/connections) lists it.
- `reason` (string): With `error`: `denied` (the person cancelled or declined, or the app refused them), `exchange_failed` (the app's sign-in did not
  complete), `not_configured` (the app cannot be connected just now), `expired` (the link expired or was already
  used) or `limit` (your plan's [connected app users](/concepts/limits#connected-app-users) were reached).
- `provider` (string): The app, as you asked for it.

The return is not proof that the app connected: anyone can open `return_to` with any query. Confirm it with
[`connections`](/api/connections) from your backend.

## Errors

- `400` (bad request): `X-Alexein-User` is missing or not a usable id.
- `401` (unauthorized): The bearer is not a key this service issued.
- `402` (payment required): Your organisation has as many app users with connected apps as its plan allows. The body has `detail`, such
  as `This plan connects apps for at most 25 app users. Disconnect one, or upgrade.`;
  `error: "quota_exceeded"`; `limit: "connections"`; `included`, the plan's figure; and `upgrade`, where an
  Owner changes the plan. An app user who already has a connected app can still connect more. See [Limits](/concepts/limits#connected-app-users).
- `403` (forbidden): `This API key only reads: ...`; a [personal key](/concepts/personal-memory#personal-keys), whose own apps
  connect under My memory › Connectors; or the organisation has been closed in the console.
- `422` (invalid): An unknown field, a `return_to` that is not `https://` (or `http://localhost` or `http://127.0.0.1`), a
  `history_days` other than 7, 15, 30 or 90, or a `provider` that cannot be connected, such as
  `jira cannot be connected`.
- `429` (too many requests): Past your organisation's rate limits. Retry after `Retry-After` seconds. See
  [Limits](/concepts/limits#rate-limits).
- `503` (unavailable): Connecting apps cannot be reached just now. Retry after `Retry-After` seconds.
```bash title="Request"
curl https://api.alexein.ai/v2/connections/connect \
  -H "Authorization: Bearer $ALEXEIN_API_KEY" \
  -H "X-Alexein-User: user_42" \
  -H "Content-Type: application/json" \
  -d '{
    "provider": "google-gmail",
    "return_to": "https://app.example.com/settings/apps",
    "history_days": 30
  }'
```

```json title="200 OK"
{
  "url": "https://app.alexein.ai/connect/oQ3v…",
  "expires_at": "2026-12-01T14:21:07Z"
}
```

```text title="Back at return_to"
https://app.example.com/settings/apps?status=connected&connection_id=6f1c2d9e-4b7a-4e3f-9a51-0c8d2e7b4f13&provider=google-gmail
https://app.example.com/settings/apps?status=error&reason=denied&provider=google-gmail
```
