# SKILL — Calling third-party APIs through the ROSS Gate OAuth Gateway

> Placeholder base URL used throughout: **`https://gateway.example.com`**
> (replace with your deployed host, e.g. `https://rossgate.heyross.ai`).

This skill describes how to call external SaaS APIs (Google Workspace, Jira,
Confluence, Slack, GitHub, LinkedIn, Notion, Dropbox, Microsoft 365, and more)
**through the gateway** instead of calling the provider directly. The model
mirrors Maton.ai.

---

## The mental model (passthrough proxy)

You **never** call the provider API directly and you **never** hold the provider's
OAuth token. Instead:

1. **You authenticate to the gateway** with *your* gateway API key:
   `Authorization: Bearer sk-gw-…` — this is **your key**, not Google's/Slack's token.
2. **You route through the gateway**:
   `https://gateway.example.com/gateway/{provider}/{native-api-path}`
   The `{provider}` prefix selects which OAuth connection to use.
3. **The gateway injects the real OAuth token** for `(your user, provider)` — refreshing
   it if expired — and forwards the request to the provider's native API.
4. **The response is passed back unchanged** (full request/response fidelity).

So a "call to Google Drive" physically goes to
`gateway.example.com/gateway/gdrive/...`, **not** to `www.googleapis.com/...`.
The provider bearer token stays server-side, in the gateway.

```
your app ──(Bearer sk-gw-…)──► gateway.example.com/gateway/gdrive/drive/v3/files
                                        │  injects Google OAuth token
                                        ▼
                                www.googleapis.com/drive/v3/files
```

---

## Setting up an AI agent to use this API — keep the key OUT of its context

If you are wiring an AI coding agent (Kiro, Claude, or any other) to call this
gateway, the one rule that matters is: **the agent must never read, print, or
be shown the raw `sk-gw-…` value.** Once a secret enters an agent's
conversation context — because you pasted it in chat, or asked the agent to
`cat` the file that holds it — it can resurface later in a log, a session
summary, or a copy-pasted example, and at that point rotating it is the only
fix. This is not a hypothetical: it is exactly how a provider secret leaked in
this project's own history, from a plain chat paste.

### Preferred: a dedicated tool holds the key, the agent never sees it

If a wrapper (a small CLI, a script) exists that already knows how to call
this gateway, point the agent at *that* instead of teaching it to build raw
HTTP calls. The wrapper process reads the key from its own environment at
startup; the agent only ever sees "the tool responded with these results",
never the header it sent.

### If the agent must build HTTP calls itself, still never let it touch the file

Do **not** ask the agent to read `.env` "to get the key" — even to check it
exists. Instead:

1. Keep the key in a `.env` file, `.gitignore`d, never opened by the agent.
2. Export it once in the **shell session you run commands from**, not the
   agent's own context:
   ```bash
   export $(grep -v '^#' .env | xargs)   # or: source .env, if it uses `export KEY=...`
   ```
3. Give the agent a script or command that reads `$ROSS_GATE_API_KEY` from
   the environment and injects it — the agent runs the script, it does not
   read the value:
   ```bash
   #!/usr/bin/env bash
   # call-gateway.sh — the agent invokes this, never opens .env directly
   curl -sS "https://gateway.example.com/gateway/$1" \
     -H "Authorization: Bearer ${ROSS_GATE_API_KEY:?not set — export it first}" \
     "${@:2}"
   ```
4. Instruct the agent explicitly, e.g. in its steering/system prompt: *"Never
   run `cat .env`, `cat` on any file matching `.env*`, or otherwise print
   environment files. Use `./call-gateway.sh <provider>/<path>` for gateway
   calls; the key is injected by the script, not by you."*

A `test -f .env` (existence check) is fine — it never returns the file's
contents. Anything that prints or returns the file's bytes is not.

### If a key does leak into a chat, log, or shared document

Rotate it immediately: mint a new key (`POST /auth/api-key` from the
dashboard, or `/admin/users/:userId/api-keys` for a service account) and
revoke the old one — self-service via `POST /me/api-keys/revoke-others`, or
`DELETE /me/api-keys/{keyId}` for one specific key. Do not wait to see if it
was "actually" misused; the revoke is nearly free, a leaked key sitting valid
is not.

---

## Onboarding (get a key + connect a provider)

1. **Get an API key** — one of:
   - Self sign-up: `POST /auth/sign-up { "email", "password" }` → returns `sk-gw-…`.
     Disabled when the operator sets `DISABLE_REGISTRATION=true` on that
     environment; if so use one of the other two paths.
     If the response has `verificationRequired: true` instead of a key, open the
     emailed link, then log in on `/dashboard` to get the key.
   - Google SSO from the dashboard (`/dashboard`) → the page mints a key for you.
     Subject to the same `DISABLE_REGISTRATION` flag for brand-new accounts;
     existing users can still sign in.
   - Admin provisioning (service-to-service, internal only): `POST /admin/provision
     { "userId" }` with header `x-admin-secret` → returns `sk-gw-…`. (`email` optional.)
2. **Connect a provider** — with your key:
   `POST /connections/connect-link { "provider": "gdrive" }` → returns a one-time
   `url` (10-min TTL, reusable within that window). Open it in a browser, approve
   the consent screen; the token is stored under your user.
3. **List your connections**: `GET /connections` → each entry has a `providerId` and an
   `accountId` (used for multi-account selection, see below).
4. **Manage your own keys** (self-service, no admin needed):
   - `GET /me/api-keys` — every key on your account, never the raw value.
   - `DELETE /me/api-keys/{keyId}` — revoke one key. Revoking the key you are
     using right now is allowed on purpose (e.g. after a leak).
   - `POST /me/api-keys/revoke-others` — keep the key in use, revoke every
     other one in a single call. The dashboard's own "sign in" flow used to
     mint a brand-new never-expiring key on every login; this is the cleanup
     for keys accumulated that way before the fix.

---

## Making calls (data plane)

```
ANY https://gateway.example.com/gateway/{provider}/{native-api-path}?{query}
Authorization: Bearer sk-gw-…
```

- `{provider}` — one of the connection ids (see the table below).
- `{native-api-path}` — the provider's own API path, verbatim.
- Method, query string, headers and body are forwarded as-is.
- Request bodies are limited to 20 MB (HTTP 413 `PAYLOAD_TOO_LARGE`); use the provider's resumable/chunked upload for larger files.

### Multiple accounts for the same provider

If you've linked more than one account for a provider (e.g. two Google accounts),
select which one with the **`X-Gateway-Connection`** header, set to the `accountId`
from `GET /connections` — **not** the connection's own `id` field, those are two
different values in the same response:

```
X-Gateway-Connection: 106688…   # the accountId of the connection you want
```

Without the header, the gateway falls back to the **oldest** linked account for
that provider (stable, deterministic — not an arbitrary pick).

---

## Provider prefixes → native API references

The gateway forwards to each provider's native API. Consult the **official** docs
(linked below) for the exact paths — the gateway does not alter them.

### Google Workspace (one shared OAuth client, one connection per service)

| Prefix (`{provider}`) | Upstream base | Native API reference (source of truth) |
|---|---|---|
| `gmail` | `gmail.googleapis.com` | https://developers.google.com/gmail/api/reference/rest |
| `gsheets` | `sheets.googleapis.com` | https://developers.google.com/sheets/api/reference/rest |
| `gdrive` | `www.googleapis.com` | https://developers.google.com/drive/api/reference/rest/v3 |
| `gcalendar` | `www.googleapis.com/calendar/v3` | https://developers.google.com/calendar/api/v3/reference |
| `gdocs` | `docs.googleapis.com` | https://developers.google.com/docs/api/reference/rest |
| `gforms` | `forms.googleapis.com` | https://developers.google.com/forms/api/reference/rest |
| `gcontacts` | `people.googleapis.com` | https://developers.google.com/people/api/rest — note: Contacts run on the **People API**, there is no `contacts.googleapis.com` |
| `gtasks` | `tasks.googleapis.com` | https://developers.google.com/tasks/reference/rest |
| `ganalytics` | `analyticsdata.googleapis.com` | https://developers.google.com/analytics/devguides/reporting/data/v1 — **read only, discovery not available, see note below** |

> **`ganalytics` known limitation, confirmed by QA:** the gateway only routes to
> the GA4 **Data API** host (`analyticsdata.googleapis.com`), never to the
> **Admin API** (`analyticsadmin.googleapis.com`). This means an agent cannot
> discover which properties exist on the connected account — any attempt at
> `v1beta/accountSummaries` or `v1beta/properties` returns an HTML 404, not a
> clean API error, because the request never reaches Google's backend. **You
> must supply the numeric GA4 property id yourself** (e.g. from
> `analytics.google.com` → Admin → Property Settings) before any report call
> can work. This is a routing limitation, not a scope problem — `analytics.readonly`
> is correctly granted.

### Atlassian (needs a Cloud ID, resolved automatically)

| Prefix | Upstream base | Native API reference |
|---|---|---|
| `jira` | `api.atlassian.com/ex/jira` | https://developer.atlassian.com/cloud/jira/platform/rest/v3 |
| `confluence` | `api.atlassian.com/ex/confluence` | https://developer.atlassian.com/cloud/confluence/rest/v2 |

> Atlassian also needs a Cloud ID in the path. The gateway resolves and caches
> it automatically on the first call, so you use the native path without it.
> If resolution fails you get one of two distinct error codes instead of a
> generic failure: `ATLASSIAN_AUTH_FAILED` (the token itself was rejected —
> reconnect) or `NO_ACCESSIBLE_SITE` (the token is valid but no site on this
> account exposes the product — check licensing/API access in the Atlassian
> admin console, the gateway cannot fix this from its side).

### Microsoft 365 (one shared Entra app, one connection per service)

| Prefix | Upstream base | Native API reference |
|---|---|---|
| `outlook` | `graph.microsoft.com` | https://learn.microsoft.com/en-us/graph/api/resources/mail-api-overview |
| `sharepoint` | `graph.microsoft.com` | https://learn.microsoft.com/en-us/graph/api/resources/sharepoint |
| `onedrive` | `graph.microsoft.com` | https://learn.microsoft.com/en-us/graph/api/resources/onedrive |
| `excel` | `graph.microsoft.com` | https://learn.microsoft.com/en-us/graph/api/resources/excel — workbook API under `/me/drive`, same host as OneDrive |
| `onenote` | `graph.microsoft.com` | https://learn.microsoft.com/en-us/graph/api/resources/onenote |

### Everything else

| Prefix | Upstream base | Native API reference |
|---|---|---|
| `slack` | `slack.com/api` | https://api.slack.com/methods |
| `github` | `api.github.com` | https://docs.github.com/en/rest |
| `linkedin` | `api.linkedin.com` | https://learn.microsoft.com/en-us/linkedin/ — only `Sign In with LinkedIn` (openid/profile/email) and `Share on LinkedIn` (`w_member_social`, post/comment as the member) are open to standard developer access; reading other members' posts (`r_member_social`) is a **closed** LinkedIn permission, not currently grantable at all |
| `notion` | `api.notion.com/v1` | https://developers.notion.com/reference |
| `dropbox` | `api.dropboxapi.com/2` | https://www.dropbox.com/developers/documentation/http/documentation |
| `spotify` | `api.spotify.com` | https://developer.spotify.com/documentation/web-api |
| `x` | `api.twitter.com` | https://developer.x.com/en/docs/x-api |
| `strava` | `www.strava.com/api/v3` | https://developers.strava.com/docs/reference/ |
| `reddit` | `oauth.reddit.com` | https://www.reddit.com/dev/api/ |
| `salesforce` | per-org `instance_url` (login host is auth-only) | https://developer.salesforce.com/docs/apis |

---

## Examples

**List Gmail messages**
```bash
curl "https://gateway.example.com/gateway/gmail/gmail/v1/users/me/messages" \
  -H "Authorization: Bearer sk-gw-…"
```

**Read a Google Sheet**
```bash
curl "https://gateway.example.com/gateway/gsheets/v4/spreadsheets/{spreadsheetId}" \
  -H "Authorization: Bearer sk-gw-…"
```

**Create a Google Doc**
```bash
curl -X POST "https://gateway.example.com/gateway/gdocs/v1/documents" \
  -H "Authorization: Bearer sk-gw-…" -H "Content-Type: application/json" \
  -d '{"title":"My doc"}'
```

**Upload a file (model) to Drive** — multipart, so you can set the name/folder:
```bash
curl -X POST "https://gateway.example.com/gateway/gdrive/upload/drive/v3/files?uploadType=multipart" \
  -H "Authorization: Bearer sk-gw-…" \
  -F "metadata={\"name\":\"model.onnx\",\"parents\":[\"<folderId>\"]};type=application/json" \
  -F "file=@model.onnx;type=application/octet-stream"
```
For files over 20 MB use `uploadType=resumable` and send the content in chunks of at most 20 MB (a multiple of 256 KiB, e.g. 16 MiB) with `Content-Range`.

**Post a Slack message (specific account)**
```bash
curl -X POST "https://gateway.example.com/gateway/slack/chat.postMessage" \
  -H "Authorization: Bearer sk-gw-…" \
  -H "X-Gateway-Connection: <accountId>" \
  -H "Content-Type: application/json" \
  -d '{"channel":"C123","text":"hello"}'
```

**Create a Google Form and read its responses**
```bash
curl -X POST "https://gateway.example.com/gateway/gforms/v1/forms" \
  -H "Authorization: Bearer sk-gw-…" -H "Content-Type: application/json" \
  -d '{"info":{"title":"Feedback"}}'

curl "https://gateway.example.com/gateway/gforms/v1/forms/{formId}/responses" \
  -H "Authorization: Bearer sk-gw-…"
```

**Export your own account data (GDPR access request)**
```bash
curl "https://gateway.example.com/me/export" -H "Authorization: Bearer sk-gw-…"
```
Returns everything the gateway holds about you as JSON — connections, keys
(prefix/last-four only), sessions. OAuth token values and key hashes are
never included: they are gateway secrets, not personal data to disclose.

Deleting your account (`DELETE /me`) revokes every key and session and removes
every provider connection at once (grants are also revoked at the provider
where possible); the profile is erased after 30 days. If support cancels the
deletion, every provider must be reconnected.

---

## Notes / limits

- **Rate limit**: 10 requests/second per API key (HTTP 429 on exceed).
- **Errors**: `401` = missing/invalid key or no connection for that provider;
  `400` = unknown provider; upstream errors are passed through unchanged. On a
  `401` mentioning "no OAuth connection found", the right move is to call
  `connect_provider` / `POST /connections/connect-link`, not to retry the same
  call — it will fail identically until the user completes the consent screen.
- **Scopes** are fixed per provider connection (least privilege). If a native call
  needs a scope the connection doesn't have, re-connect the provider — the
  gateway returns a clean 403 from the upstream provider, it does not invent
  data or silently no-op.
- The provider OAuth token is never returned to the client — only the gateway sees it.
- The admin plane (`/admin/*`) is intentionally **not documented in the public
  OpenAPI/Swagger UI** even though it exists — it is internal-only. Do not rely
  on `/docs` to discover it; this file is the closest thing to a spec for it.
