# Start connecting a directory source through OAuth

<div class="api-op" data-api-operation>
<div class="api-op__main">

<p class="api-endpoint"><span class="method method-post">POST</span> <code>/v1/directories/&#123;dir&#125;/source/oauth/start</code></p>

Starts the OAuth connection of a `gusto`, `bamboohr` or `google_admin_sdk` directory through the OhAuthCat app of that provider (provider `google` for `google_admin_sdk`: Connect Google). Gusto: authorization code with a PKCE S256 challenge. Google: authorization code with PKCE S256 at accounts.google.com, `access_type=offline`, `prompt=consent`, scopes openid, email and the Admin SDK Directory user, group and group member read-only scopes; optional `domain` is only the `hd` hint. Google Workspace has no service-account key connection. BambooHR: authorization code on the company's domain (`subdomain` is required), scopes openid, email, offline_access, employee, employee:name, employee:job, employee:contact and employee_directory; BambooHR documents no PKCE, so none is sent. BambooHR has no API-key connection. Send the IT admin's browser to `authorization_url` within 30 minutes: it is the OhAuthCat confirm page (GET /v1/directory_sources/oauth/confirm), which names the organization and environment and continues to the provider only after the person presses Continue; a callback for a flow that was never confirmed answers `directory_source_oauth_confirm_required`. For Google, the approving Workspace domain (`hd`) must be a verified domain of the directory's organization, else `directory_source_oauth_domain_unverified` and nothing is stored. Gusto returns to the OhAuthCat callback, which exchanges the code, stores the tokens sealed, saves the source and redirects to `return_url` with `ohauthcat_oauth` set to `connected`, `choose_company` or `error` (with `error_code`) and `directory_id`. The state is bound to this environment and directory and works once. Answers service_unavailable when the Gusto OAuth app is not configured; a directory of another provider answers `directory_source_mismatch`.

<dl class="api-facts"><div><dt>Scope</dt><dd><code>directories:write</code></dd></div><div><dt>Auth</dt><dd>Secret key (<code>sk_…</code>)</dd></div><div><dt>Since</dt><dd>1.0</dd></div></dl>

## Path parameters

<ul class="api-fields">
<li class="api-field">
<p class="api-field__head"><code class="api-field__name">dir</code> <span class="api-field__type">string</span> <span class="api-field__required">Required</span></p>
</li>
</ul>

## Headers

<ul class="api-fields">
<li class="api-field">
<p class="api-field__head"><code class="api-field__name">OhAuthCat-Version</code> <span class="api-field__type">string</span></p>
Minor version; defaults to the environment's pin.
</li>
</ul>

## Request body

<ul class="api-fields">
<li class="api-field">
<p class="api-field__head"><code class="api-field__name">return_url</code> <span class="api-field__type">string (uri)</span> <span class="api-field__required">Required</span></p>
Where the callback sends the browser afterwards: an https URL (http only for localhost in local deployments), without a fragment.

<p class="api-field__chips">max length 2000</p>
</li>
<li class="api-field">
<p class="api-field__head"><code class="api-field__name">subdomain</code> <span class="api-field__type">string</span></p>
Required for a `bamboohr` directory: the company's BambooHR domain, the part before `.bamboohr.com`. BambooHR's sign-in page is on that domain. Ignored for `gusto` and `google_admin_sdk`.
</li>
<li class="api-field">
<p class="api-field__head"><code class="api-field__name">domain</code> <span class="api-field__type">string</span></p>
Optional for a `google_admin_sdk` directory: the Google Workspace domain, sent to Google only as the `hd` sign-in hint. Never trusted for identity: the Workspace domain is taken from the verified ID token. Ignored for other providers.

<p class="api-field__chips">max length 253</p>
</li>
</ul>

## Responses

<ul class="api-responses">
<li class="api-response"><span class="api-status api-status--ok">201</span> <span>Created <span class="api-field__type">DirectorySourceOAuthStart</span></span></li>
<li class="api-response"><span class="api-status api-status--bad">400</span> <span>Invalid request. Codes: <a href="/errors/parameter_invalid/">parameter_invalid</a>, <a href="/errors/parameter_missing/">parameter_missing</a>, <a href="/errors/version_unsupported/">version_unsupported</a> and resource-specific codes from the catalogue. <span class="api-field__type">Error</span></span></li>
<li class="api-response"><span class="api-status api-status--bad">401</span> <span>Codes: <a href="/errors/authentication_required/">authentication_required</a>, <a href="/errors/api_key_invalid/">api_key_invalid</a>. <span class="api-field__type">Error</span></span></li>
<li class="api-response"><span class="api-status api-status--bad">403</span> <span>Codes: <a href="/errors/api_key_scope_missing/">api_key_scope_missing</a>, <a href="/errors/ip_not_allowed/">ip_not_allowed</a>, <a href="/errors/plan_limit_reached/">plan_limit_reached</a>. <span class="api-field__type">Error</span></span></li>
<li class="api-response"><span class="api-status api-status--bad">404</span> <span>Code: <a href="/errors/resource_not_found/">resource_not_found</a> (also returned for other tenants' IDs); a path no endpoint serves answers <a href="/errors/route_not_found/">route_not_found</a>. A browser GET or HEAD of an unknown path whose Accept ranks text/html above JSON gets an HTML 404 page instead, with the same status; every other client gets this JSON body. <span class="api-field__type">Error</span></span></li>
<li class="api-response"><span class="api-status api-status--bad">409</span> <span>Codes: <a href="/errors/idempotency_key_reused/">idempotency_key_reused</a> and resource-specific conflicts. <span class="api-field__type">Error</span></span></li>
<li class="api-response"><span class="api-status api-status--bad">429</span> <span>Code: <a href="/errors/rate_limited/">rate_limited</a>. Retry-After is set. <span class="api-field__type">Error</span></span></li>
<li class="api-response"><span class="api-status api-status--bad">503</span> <span>Code: <a href="/errors/service_unavailable/">service_unavailable</a>. Retry-After is set; writes are paused. <span class="api-field__type">Error</span></span></li>
<li class="api-response"><span class="api-status api-status--neutral">default</span> <span>Any other error, including <a href="/errors/internal_error/">internal_error</a>. Every code is in the error catalogue. <span class="api-field__type">Error</span></span></li>
</ul>

Errors share one shape; see the [error code catalogue](/errors/).

</div>
<div class="api-op__code">

**curl**

```sh wrap
curl -X POST \
  "https://api.ohauthcat.com/v1/directories/dir_01J9Z3K5V7W8X9Y0Z1A2B3C4D5/source/oauth/start" \
  -H "Authorization: Bearer $OHAUTHCAT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "return_url": "https://app.acme.com/callback"
}'
```

**Node**

```javascript wrap
const response = await fetch(
  "https://api.ohauthcat.com/v1/directories/dir_01J9Z3K5V7W8X9Y0Z1A2B3C4D5/source/oauth/start",
  {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.OHAUTHCAT_API_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      return_url: "https://app.acme.com/callback",
    }),
  },
);
const data = await response.json();
```

**Python**

```python wrap
import os
import requests

response = requests.post(
    "https://api.ohauthcat.com/v1/directories/dir_01J9Z3K5V7W8X9Y0Z1A2B3C4D5/source/oauth/start",
    headers={"Authorization": f"Bearer {os.environ['OHAUTHCAT_API_KEY']}"},
    json={
        "return_url": "https://app.acme.com/callback",
    },
)
data = response.json()
```

```json title="Response 201" wrap
{
  "object": "directory_source_oauth_start",
  "directory_id": "dir_01J9Z3K5V7W8X9Y0Z1A2B3C4D5",
  "provider": "gusto",
  "authorization_url": "https://app.acme.com/callback",
  "expires_at": "2026-01-15T09:30:00Z"
}
```

</div>
</div>
