# auth.md — HiRey Hi

You are an AI agent. This file tells you how to authenticate to **HiRey Hi**, the people-to-people
platform you use on your user's behalf (jobs and candidates, landlords and tenants, friends, dates,
cofounders, investors, lawyers, and any other human lead).

Reading and searching are **anonymous and free**. Writing (profile, listings, contacting people,
messaging, scheduling) requires the person behind the agent to **sign in once** (phone, email or Google).

- Resource server (API): `https://hi.hirey.ai`
- MCP server: `https://mcp.hirey.ai/mcp` (Streamable HTTP)
- Authorization server: `https://hi.hirey.ai`
- Scope: `hirey.agent`

The shortest path: get an API key with one anonymous `POST https://hi.hirey.ai/v1/agents/api-keys`
and send it as `Authorization: Bearer <api_key>` (Step 2a). OAuth access tokens from
`https://hi.hirey.ai` also work (Step 2c).

Discovery metadata:

- Protected Resource Metadata — `https://mcp.hirey.ai/.well-known/oauth-protected-resource`
- Authorization Server Metadata — `https://hi.hirey.ai/.well-known/oauth-authorization-server`

## Step 1 — Discover

Fetch the Protected Resource Metadata, then the Authorization Server Metadata it points to:

```http
GET https://mcp.hirey.ai/.well-known/oauth-protected-resource
GET https://hi.hirey.ai/.well-known/oauth-authorization-server
```

The `authorization_servers` field gives you `https://hi.hirey.ai`, and `scopes_supported` gives you
`hirey.agent`. The authorization-server document lists the registration, authorization and token
endpoints. You only need them for OAuth (Step 2c); an API key (Step 2a) needs no discovery.

## Step 2 — Get an identity

Pick one. All three give you a bearer credential for the same API.

**a. API key — one call, no installer (recommended; works on any host that can send HTTPS).**
This creates no user account (only an anonymous connection record), sends no personal data and costs nothing:

```http
POST https://hi.hirey.ai/v1/agents/api-keys
Content-Type: application/json

{ "display_name": "<your agent name>", "agent_type": "<optional host name>", "device_name": "<your agent name>" }
```

The `201` response holds `agent_id`, `agent_type`, `status`, `provider`, `login`,
`anonymous_operations`, `setup`, `mcp_url` and a non-rotating `api_key` (`hi_ak_…`). Send the
`api_key` itself as `Authorization: Bearer <api_key>` on capability calls and on the MCP endpoint;
there is no token exchange. (The event routes in Step 5 need an access token.)
`agent_type` values `claude`, `codex`, `openclaw` and `hermes` name a known host; any other value
(such as `muse`) registers a generic Agent. Until the user signs in (Step 3), the key can call
only its `anonymous_operations`: `people.find`, `people.explain` and `capture.record` (a capture
is staged until sign-in). Store it in your secure credential store (or a private file only you
can read), create it once and reuse it. `device_name` is the name the user sees for this connection
on https://hirey.ai/me (Muse sends `{"display_name":"Muse","agent_type":"muse","device_name":"Muse"}`).
An agent calling over HTTPS ignores the parts of the response meant for Codex or MCP clients:
`setup` (including `codex_config_toml` and "relaunch Codex"), `mcp_url`, and MCP tool names such
as `google_link` in `login`. Muse, Instinct and other agents without an installer: see
https://hirey.ai/docs/agents/setup.md.

**b. Host installer.** Claude Code, Codex, Hermes and OpenClaw have their own install paths
(skills or plugins), one block per host at https://hirey.ai/docs/agents/install.md. For example, Claude Code:

```sh
curl -fsSL https://hi.hirey.ai/v1/install/claude.sh | bash
```

**c. OAuth 2.1 (authorization code + PKCE, with Dynamic Client Registration, RFC 7591).**
MCP clients that connect to `https://mcp.hirey.ai/mcp` do this for you. The user signs in in a
browser. If you can't open one, use the API key (2a). To run your own OAuth client, register it:

```http
POST https://hi.hirey.ai/oauth/register
Content-Type: application/json

{ "client_name": "<your agent>", "redirect_uris": ["http://127.0.0.1:<port>/callback"],
  "scope": "hirey.agent" }
```

`redirect_uris` is required. Use a loopback URL (`127.0.0.1` or `localhost`) or an HTTPS callback
on a host HiRey already allows. You get a public client: `token_endpoint_auth_method` is `none`,
`grant_types` are `authorization_code` and `refresh_token`, and there is no `client_secret`. Send
the user to `https://hi.hirey.ai/oauth/authorize` with `response_type=code`, your `client_id`,
`redirect_uri`, `scope=hirey.agent`, `state`, `code_challenge` and `code_challenge_method=S256`,
then exchange the code:

```http
POST https://hi.hirey.ai/oauth/token
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code&code=<code>&redirect_uri=<redirect_uri>&client_id=<client_id>&code_verifier=<verifier>
```

Renew with `grant_type=refresh_token&refresh_token=<refresh_token>&client_id=<client_id>`; each
refresh returns a new refresh token. Registered clients can't use `client_credentials`; the
no-browser path is the API key (2a).

## Step 3 — Sign in (right after the first search)

An anonymous identity can read and search immediately. Introducing the user and publishing their
Page, photos, posting, contacting, messaging and scheduling return `verified_login_required` until
the person behind the agent signs in once, so ask right after the first search.
Ask which sign-in they already use with HiRey (phone, email or Google), so they don't end up
with a second account:

```http
POST https://hi.hirey.ai/v1/capabilities/hi.phone-binding/call
Authorization: Bearer <api_key or access_token>
Content-Type: application/json

{ "action": "bind", "phone": "<E.164>" }
# then:
{ "action": "verify", "code": "<sms code>", "display_name": "<their full name>" }
```

`display_name` is the name people see when the user connects with them. Use the full name the user gave you; if you don't know it, ask. Never guess it, for example from an email address.

Email works the same with `hi.email-binding` and `"email"`. Google: `hi.google-link` with
`{ "action": "start" }`; the user opens `verification_url` on their own device, then call
`{ "action": "poll" }` until `status` is `verified`. The same credential keeps working
afterwards; there is no reinstall.

After sign-in, first check whether the user already has a Page (`identity.me`, then `people.find` on
their full name). Only if they have none, offer once to introduce them with their own Page. When they
send a photo taken with someone on HiRey, connect them; when HiRey can, that person also gets a private
card to confirm (HiRey lets them know, or the user texts them the link). Both publish only after the
user says yes. The exact calls: https://hirey.ai/docs/agents/page.md and
https://hirey.ai/docs/agents/photo-with-someone.md; the whole setup in one go:
https://hirey.ai/docs/agents/setup.md.

## Step 4 — Use the API

Send the API key or access token as a bearer header:

```http
GET  https://hi.hirey.ai/v1/capabilities                       Authorization: Bearer <api_key or access_token>
POST https://hi.hirey.ai/v1/capabilities/<capability_id>/call  Authorization: Bearer <api_key or access_token>
```

`GET /v1/capabilities` returns the live machine-readable tool catalog. Most work goes through
`hi.workspace-workflows`: `{"action":"catalog"}` lists every operation and
`{"action":"describe","payload":{"operation":"<name>"}}` shows one operation's fields.

## Step 5 — Replies and inbound events

**To check for replies, call `message.inbox` on `hi.workspace-workflows`** (Step 4) with the same
API key or access token. `agent_message.list` reads the user's wider inbox (messages, tasks and
events for their Agents). Both need the user to be signed in (Step 3), and reading changes nothing.

The event queue below is a delivery signal only. It may be empty even when replies are waiting, so
don't treat an empty queue as "no replies". Events are **pulled**: claim one, handle it, acknowledge
it. No public URL is needed, and HiRey does not push events to a webhook URL.

These routes take a signed access token, not the raw API key. With an API key, get a token from
`POST https://hi.hirey.ai/oauth/token` (form body
`grant_type=client_credentials&client_id=<id>&client_secret=<secret>`), using the `id` and `secret`
inside the key: the part after `hi_ak_` is base64url JSON `{"v":1,"id":…,"secret":…}`. The token
lasts `expires_in` seconds (about an hour); get a new one the same way. This works only after the
user has signed in (Step 3): before sign-in the key's token covers searching only, and these routes reject it.

```http
# 1. Claim the next event. Use a fresh idempotency_key per claim (a UUID).
POST https://hi.hirey.ai/v1/agent-events/claim
Authorization: Bearer <access_token>
Content-Type: application/json

{ "idempotency_key": "<uuid>" }

# 2. Optional: read an event again by id (the claim already returns it)
GET https://hi.hirey.ai/v1/agent-events/<event_id>
Authorization: Bearer <access_token>

# 3. Acknowledge it before the lease ends
POST https://hi.hirey.ai/v1/agent-events/ack
Authorization: Bearer <access_token>
Content-Type: application/json

{ "event_id": "<event_id>", "lease_token": "<lease_token from the claim>", "outcome": "delivered" }
```

- Every successful response is wrapped as `{ "result": … }`; errors are `{ "error": "<code>" }`.
  A claim returns `{ "result": { "event": {…}, "repeated": false } }`, or `"event": null` when nothing
  is waiting. Claim again until it is `null`; each claim returns one event.
- An event carries `event_id`, `event_seq`, `topic`, `resource_ref`, `preview`, `payload`, `status`,
  `attempts`, `created_at`, `lease_expires_at` and, on a claim, `lease_token`.
- The lease lasts about 60 seconds. Repeating a claim with the same `idempotency_key` while the lease is
  live returns the same event with `"repeated": true`. An event you don't acknowledge in time is offered again.
- `outcome` is `delivered` (done) or `failed` (offered again after 30 seconds, until its attempts run out;
  you may add an `error_code`). An ack with an expired or wrong `lease_token` returns `409 event_lease_invalid`.

**Endpoint and subscription records.** An Agent can also store its endpoints and topic subscriptions.
Each `PUT` replaces the whole list, and the matching `GET` returns it. These records do not change delivery
today: every event is still pulled as above.

```http
PUT https://hi.hirey.ai/v1/agents/me/endpoints
Authorization: Bearer <access_token>
Content-Type: application/json

{ "endpoints": [ { "kind": "pull_stream", "profile": "<name>", "status": "active" } ] }

PUT https://hi.hirey.ai/v1/agents/me/subscriptions
Authorization: Bearer <access_token>
Content-Type: application/json

{ "subscriptions": [ { "topic": "<topic>", "status": "active" } ] }
```

- An endpoint needs `kind` (`webhook`, `http_api` or `pull_stream`) and `profile`; `url`, `auth`, `config`,
  `capability_ids`, `status` (`active` or `disabled`) and an existing `endpoint_id` are optional. Up to 50.
- A subscription needs a `topic` (each topic once); `status`, `filter` and an existing `subscription_id`
  are optional. Up to 100.
- `auth` and `config` are stored as given and returned by the `GET`, so don't put secrets in them.

## Claude desktop app / claude.ai web (custom connector)

The Claude desktop app and claude.ai on the web connect to `https://mcp.hirey.ai/mcp` through their built-in
**Add custom connector** flow (Settings → Connectors). They perform Dynamic Client Registration and the
PKCE `S256` authorization-code dance automatically against `https://hi.hirey.ai` — you do not hand-register
a client or paste a token. The desktop app uses a loopback (`127.0.0.1`) OAuth callback; the web app's callback
is `https://claude.ai/api/mcp/auth_callback` (migrating to `claude.com`). Both callback hosts must be present on
the authorization server's allowed-redirect-host list for the web sign-in to complete. A connector added on web or
desktop syncs to the Claude mobile app on next sign-in.

## Notes

- Tokens are sent only in the `Authorization: Bearer` header.
- Keep the `hi_ak_…` API key secret; treat it like a password. Never paste it into chat or logs.
- Full agent-facing spec, per-host install, and examples: https://hirey.ai/llms.txt
