# Connector Helper

Source: https://docs.autessa.com/docs/autessascript/helpers/connector

> The connector helper: pull OAuth tokens, API keys, and metadata from your connected accounts to call third-party APIs directly.

# Connector Helper

The `connector` helper hands your script the live credentials behind a connected account — Gmail, Notion, QuickBooks, or any other connector your company has linked — so you can call the vendor's API directly with `api.*`. Token refresh is handled for you: if the access token is stale, the helper refreshes it before returning.

All methods are company-scoped: a script can only see connectors belonging to the company it runs in, and the helper requires the company context that tool execution provides. In contexts without it (such as [computed columns](/docs/autessascript/computed-columns.md)), calls return a `NO_COMPANY_CONTEXT` error.

## Methods

  - `connector.getAuth` — string"> Everything needed for an authenticated call: access token, base URL (when the connector type has one), connector metadata, and any type-specific extra properties. Refreshes the token first if needed.
  - `connector.getAccessToken` — string"> Just the access token (or API key), refreshed if needed. Lighter envelope when you already know the endpoint.
  - `connector.getMetadata` — string"> The connector's type-specific metadata (account info, base URL, realm ids, and so on) without touching credentials.
  - `connector.listConnectors` — string"> All active connectors for the current company — use this to discover connector ids from inside a script.

## Response shapes

Every method returns a JSON string with `success` plus method-specific fields.

`connector.getAuth(connectorId)`:

```json
{
  "success": true,
  "connectorType": "GMAIL",
  "connectorName": "Support Inbox",
  "accessToken": "ya29.a0Af...",
  "baseUrl": "https://gmail.googleapis.com",
  "additionalProperties": { "...": "type-specific extras when present" },
  "metadata": { "...": "type-specific connector metadata when present" }
}
```

`baseUrl`, `additionalProperties`, and `metadata` are present only when the connector type provides them — check before using.

`connector.getAccessToken(connectorId)`:

```json
{ "success": true, "accessToken": "ya29.a0Af..." }
```

`connector.getMetadata(connectorId)`:

```json
{ "success": true, "connectorType": "QUICKBOOKS", "metadata": { "...": "..." } }
```

`connector.listConnectors()`:

```json
{
  "success": true,
  "connectors": [
    { "connectorId": "cK2vB9xD", "connectorName": "Support Inbox", "connectorType": "GMAIL", "isActive": true },
    { "connectorId": "cH7wN4mJ", "connectorName": "Main CRM", "connectorType": "ZOHO_CRM", "isActive": true }
  ]
}
```

## Error codes

Failures return `{ "success": false, "errorCode": "...", "errorMessage": "..." }`:

  - `CONNECTOR_NOT_FOUND` (errorCode) — No connector with that id in this company. Call `connector.listConnectors()` to see what exists.
  - `CONNECTOR_INACTIVE` (errorCode) — The connector is disabled — re-enable it in settings.
  - `REFRESH_TOKEN_EXPIRED` (errorCode) — The refresh token was revoked upstream; the connector must be re-authenticated by a user.
  - `TOKEN_REFRESH_FAILED` (errorCode) — The refresh attempt failed for another reason (transient upstream errors included).
  - `NO_CREDENTIALS` (errorCode) — The connector exists but holds no usable access token for this type.
  - `NO_COMPANY_CONTEXT` (errorCode) — The script isn't running with a company context (e.g. a computed column) — connector access requires tool execution.
  - `SERVICE_NOT_INITIALIZED` (errorCode) — Connector services weren't available yet; retry later.
  - `INTERNAL_ERROR` (errorCode) — Unexpected failure; `errorMessage` carries details.

## Example: call the Gmail API with a connector token

`getAuth` plus `api.get` is the whole pattern — the token goes straight into an `Authorization` header:

```javascript
const auth = JSON.parse(connector.getAuth(12));   // your Gmail connector's id
if (!auth.success) {
  response.setResponse(JSON.stringify({ error: auth.errorCode + ": " + auth.errorMessage }));
} else {
  const res = JSON.parse(api.get(JSON.stringify({
    endpoint: "https://gmail.googleapis.com/gmail/v1/users/me/messages?q=is:unread&maxResults=5",
    headers: { "Authorization": "Bearer " + auth.accessToken }
  })));

  if (res.statusCode !== 200) {
    response.setResponse(JSON.stringify({ error: "Gmail returned " + res.statusCode }));
  } else {
    const inbox = JSON.parse(res.body);
    console.log("unread messages:", inbox.resultSizeEstimate);
    response.setResponse(JSON.stringify({
      unread: inbox.resultSizeEstimate,
      messageIds: (inbox.messages || []).map(m => m.id)
    }));
  }
}
```

> Prefer a dedicated connector tool type when one exists for your operation — it handles the vendor's API shape for you. The `connector` helper is for the gaps: endpoints no tool type covers yet, or vendor-specific parameters a config can't express.

## Related

- [API helper](/docs/autessascript/helpers/api.md)
- [Tool invocation helper](/docs/autessascript/helpers/tools.md)
- [Examples](/docs/autessascript/examples.md)
- [AutessaScript overview](/docs/autessascript.md)

