> For the complete documentation index, see [llms.txt](https://docs.erathos.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.erathos.com/api/introduction/set-up-connections.md).

# Set up connections

Set up a workspace's connections, jobs and destination through the API, from choosing a connector to the first run.

{% hint style="info" %}
This feature is in Beta.
{% endhint %}

Everything you set up in the platform — a connection, its jobs, the workspace's destination — can also be set up through the API. This lets you build Erathos into your own product: for example, to create a connection for each of your customers from your own onboarding flow, without them ever opening the platform.

The setup follows the same steps as in the platform:

1. [**Find your workspace**](#1-find-your-workspace)
2. [**Create a connection**](#2-create-a-connection) and send its credentials
3. [**Configure its jobs**](#3-configure-its-jobs) — the tables or endpoints to sync, and how
4. [**Create the destination**](#4-create-the-destination) the data is loaded into
5. [**Run the jobs**](#5-run-the-jobs) and follow their runs

## Before you start

* **Authentication.** Every request carries an API key in the `Authorization` header (`Api-Key <API_KEY>`). See [Authentication](/api/concepts/authentication.md). The key acts as its user: each request needs the permissions that user has in the workspace.
* **Plan.** The setup endpoints need the **Developer API** feature in the workspace's plan. Without it, they answer `402 Payment Required`.
* **Errors.** When a step can't go ahead — the credentials aren't valid yet, a setting is missing, the connector isn't available — the API answers `400` with a `message` that says what to do next.

The examples below use `https://api.erathos.com` and these variables:

```bash
export ERATHOS_API_KEY="<API_KEY>"
export WORKSPACE_ID="<workspace id>"
```

## 1. Find your workspace

Connections are created in an existing workspace. List the workspaces the API key's user belongs to and take the `_id` of the one to use:

```bash
curl https://api.erathos.com/developers/workspaces/ \
  -H "Authorization: Api-Key $ERATHOS_API_KEY"
```

See [Workspaces](/api/api-reference/workspaces.md).

## 2. Create a connection

### Choose a connector

A connection is created on a **connector** — the type of data source, such as HubSpot or PostgreSQL. List the connectors the workspace can use (filter with `name`, `is_api` or `is_database`):

```bash
curl "https://api.erathos.com/developers/workspaces/$WORKSPACE_ID/source-connectors/?name=hubspot" \
  -H "Authorization: Api-Key $ERATHOS_API_KEY"
```

A connector's detail lists the credentials a connection on it takes, so you can collect them before creating anything:

* `auth_fields` and `advanced_auth_fields`: values such as a host, a user or an API token. `is_required` says which are mandatory; `is_hidden` marks a secret, whose value is never returned.
* `connector_files`: files such as a certificate or a service account key.
* `oauth_fields`: accounts the end user signs in to with the provider (see [OAuth](#oauth)).

```bash
curl https://api.erathos.com/developers/workspaces/$WORKSPACE_ID/source-connectors/<connector_id>/ \
  -H "Authorization: Api-Key $ERATHOS_API_KEY"
```

### Create it

```bash
curl -X POST https://api.erathos.com/developers/workspaces/$WORKSPACE_ID/connections/ \
  -H "Authorization: Api-Key $ERATHOS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"connector_id": "<connector_id>", "name": "HubSpot - Acme"}'
```

The connection is created without credentials. The response is the connection as [Get connection](/api/api-reference/connections.md#connections) returns it, with `setup_status: awaiting_credentials`. Keep its `connection_id` for every step that follows.

### Send its credentials

Send the values by their field `_id`. Files go base64-encoded. The first time, send every required field. After that, only what you send changes: a field left out keeps its saved value, so you can also use this to replace a single expired token later.

```bash
curl -X PATCH https://api.erathos.com/developers/workspaces/$WORKSPACE_ID/connections/<connection_id>/credentials/ \
  -H "Authorization: Api-Key $ERATHOS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "auth_fields": [
      {"_id": "<auth_field_id>", "value": "<api token>"}
    ],
    "connector_files": [
      {"_id": "<connector_file_id>", "filename": "key.json", "value": "<base64 content>"}
    ]
  }'
```

The credentials are checked in the background: `has_valid_credentials` is `null` until the check finishes, then `true` or `false`.

#### OAuth

Some connectors don't take a token: the end user signs in with the provider instead. For each of the connection's `oauth_fields`, start an authorization:

```bash
curl -X POST https://api.erathos.com/developers/workspaces/$WORKSPACE_ID/connections/<connection_id>/oauth/<oauth_field_id>/authorizations/ \
  -H "Authorization: Api-Key $ERATHOS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"return_url": "https://app.example.com/erathos/callback"}'
```

Send the end user to the `authorization_url` in the response. They sign in with the provider, Erathos receives the authorization and saves it on the connection, and then:

* **With a `return_url`**, the end user is sent back to it, with `status` (`success` or `error`), `connection_id`, `authorization_id` and, on failure, an `error` code added to its query string. The `return_url` must use `https` (`http` is accepted only for `localhost`).
* **Without one**, they see a page saying the authorization is done and they can close the window.

The `authorization_url` is valid for 30 minutes and can be used once. You can also follow the authorization's `status` (`pending`, `success` or `error`) with [Get OAuth authorization](/api/api-reference/connections.md#credentials).

{% hint style="info" %}
For a connector that uses your own OAuth app, save the app's credentials (its `auth_fields`, such as its client id) before starting the authorization. Until then, starting one answers `400` with the fields still missing.
{% endhint %}

### Follow the setup status

A connection's `setup_status` says which step is next. Poll it after sending credentials or settings:

```bash
curl https://api.erathos.com/developers/workspaces/$WORKSPACE_ID/connections/<connection_id>/ \
  -H "Authorization: Api-Key $ERATHOS_API_KEY"
```

| `setup_status`         | What to do                                                                                                 |
| ---------------------- | ---------------------------------------------------------------------------------------------------------- |
| `awaiting_credentials` | Send the credentials.                                                                                      |
| `validating`           | Wait: a background check is running, such as reading the source's tables. `running_validation` says which. |
| `lost_authentication`  | The credentials stopped working: send new ones.                                                            |
| `needs_settings`       | Choose the connection's settings.                                                                          |
| `no_options_available` | The credentials give access to nothing to choose for a setting (e.g. no ad account).                       |
| `schema_unavailable`   | The source's tables or endpoints couldn't be read, or none were found.                                     |
| `ready_for_jobs`       | Configure its jobs.                                                                                        |
| `no_destination`       | The jobs are set up, but the workspace has no working destination yet.                                     |
| `configured`           | All set.                                                                                                   |

### Choose its settings

Some connectors need a choice before their data can be read — for example, which ad account or which spreadsheet. List them once the credentials are valid:

```bash
curl https://api.erathos.com/developers/workspaces/$WORKSPACE_ID/connections/<connection_id>/settings/ \
  -H "Authorization: Api-Key $ERATHOS_API_KEY"
```

For a choice setting, `options` lists what can be picked (paged per setting with `limit`/`offset`; `name` searches them). Then send every required setting:

```bash
curl -X POST https://api.erathos.com/developers/workspaces/$WORKSPACE_ID/connections/<connection_id>/settings/ \
  -H "Authorization: Api-Key $ERATHOS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"settings": [{"setting_id": "<setting_id>", "values": ["<option id>"]}]}'
```

{% hint style="warning" %}
Most settings can't change once chosen (`edit_mode: locked`): a different ad account means a new connection. Settings with `edit_mode: append`, such as a list of spreadsheet URLs, can only gain values — send just the new ones.
{% endhint %}

A connector with no settings returns none: go straight to its jobs.

## 3. Configure its jobs

A **job** syncs one table (database connections) or one endpoint (API connections) on a schedule.

### See what can be synced

```bash
# Database connections: tables, their schemas (tables/schemas/) and columns (tables/columns/)
curl https://api.erathos.com/developers/workspaces/$WORKSPACE_ID/connections/<connection_id>/tables/ \
  -H "Authorization: Api-Key $ERATHOS_API_KEY"

# API connections: endpoints and their fields
curl https://api.erathos.com/developers/workspaces/$WORKSPACE_ID/connections/<connection_id>/endpoints/ \
  -H "Authorization: Api-Key $ERATHOS_API_KEY"
```

The available sync frequencies and sync types are listed by [Sync options](/api/api-reference/sync-options.md).

### Preview, then save

Configuring jobs applies one set of settings to a selection of tables or endpoints — by id (`table_ids`, `endpoint_ids`), by schema (`schema_names`), by name (`name_contains`) or everything (`select_all`). Every setting is optional: a new job gets the recommended one (syncing daily at 03:00), and an existing job keeps its own. Call it again for groups with other settings.

It's a **dry run by default**: the response shows the jobs that would result, without saving them.

```bash
curl -X POST https://api.erathos.com/developers/workspaces/$WORKSPACE_ID/connections/<connection_id>/jobs/configure/ \
  -H "Authorization: Api-Key $ERATHOS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "endpoint_ids": ["<deals endpoint_id>", "<contacts endpoint_id>"],
    "sync_type": "recommended",
    "sync_frequency_id": "<sync_frequency_id>",
    "sync_time": "06:00"
  }'
```

Check the response before saving:

* `pipeline` lists the resulting job groups, with their sync type and schedule.
* `needs_decision` lists the tables that need a choice, such as the cursor column to sync incrementally.
* `errors` lists the tables or endpoints that couldn't be configured, and why.

Then send the same request with `"dry_run": false` to save it.

{% hint style="info" %}
An incremental sync needs a primary key and a cursor column. `recommended` syncs incrementally where possible and in full elsewhere. When you ask for an incremental sync, tables that can't have one are left out and listed in `needs_decision` — unless you send `"on_incremental_unavailable": "full"`, which includes them as full syncs on the `fallback_sync_frequency_id`/`fallback_sync_time` schedule.
{% endhint %}

Jobs can later be switched off and on with [Disable jobs and Enable jobs](/api/api-reference/connections.md#jobs). Disabling a job also disables the jobs that depend on it; enabling one also enables the jobs it depends on.

## 4. Create the destination

The **destination** is the data warehouse the jobs load into. A workspace has one, shared by all its connections. List the destination connectors, create the destination and send its credentials — the same way as for a connection:

```bash
curl https://api.erathos.com/developers/workspaces/$WORKSPACE_ID/destination-connectors/ \
  -H "Authorization: Api-Key $ERATHOS_API_KEY"

curl -X POST https://api.erathos.com/developers/workspaces/$WORKSPACE_ID/destination/ \
  -H "Authorization: Api-Key $ERATHOS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"connector_id": "<connector_id>", "name": "BigQuery"}'

curl -X PATCH https://api.erathos.com/developers/workspaces/$WORKSPACE_ID/destination/credentials/ \
  -H "Authorization: Api-Key $ERATHOS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"auth_fields": [{"_id": "<auth_field_id>", "value": "<value>"}]}'
```

Check whether the workspace already has a destination with [Get destination](/api/api-reference/destination.md): if it does, skip this step. Jobs load data only once its `has_valid_credentials` and `has_valid_schema` are both `true`.

The destination can be created at any point before the first run: jobs can be configured without one.

## 5. Run the jobs

Jobs run on their schedule. To start a run right away — for example, the first load after setup:

```bash
curl -X POST https://api.erathos.com/developers/workspaces/$WORKSPACE_ID/connections/<connection_id>/run/ \
  -H "Authorization: Api-Key $ERATHOS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{}'
```

This queues all the connection's active jobs; send `job_ids` to run only some. A run can't start while the workspace has no working destination.

Follow the runs by connection or by job:

```bash
curl "https://api.erathos.com/developers/workspaces/$WORKSPACE_ID/runs/?connection_id=<connection_id>" \
  -H "Authorization: Api-Key $ERATHOS_API_KEY"
```

`FINISHED`, `FAILED` and `CANCELED` are final; `QUEUED`, `WAITING`, `EXECUTING` and `BLOCKED` are still in progress. A run's detail adds its metrics, logs and timeline. To stop the connection's runs in progress, use [Cancel connection runs](/api/api-reference/connections.md#runs).

{% hint style="info" %}
To trigger work in other tools when a run finishes, see [Trigger jobs outside Erathos](/api/introduction/trigger-jobs-outside-erathos.md).
{% endhint %}

## API Reference

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td>Connectors</td><td><a href="/api/api-reference/connectors.md">Connectors</a></td></tr><tr><td>Connections</td><td><a href="/api/api-reference/connections.md">Connections</a></td></tr><tr><td>Destination</td><td><a href="/api/api-reference/destination.md">Destination</a></td></tr><tr><td>Runs</td><td><a href="/api/api-reference/runs.md">Runs</a></td></tr><tr><td>Sync options</td><td><a href="/api/api-reference/sync-options.md">Sync options</a></td></tr></tbody></table>
