# SearchSoftware Public API

The **SearchSoftware Public API** is a REST interface for managing records in your SearchSoftware environment from external integrations.

## Authentication

Choose an API key for a server integration with workspace-wide access, or OAuth when your app acts on behalf of a user. Browser and desktop apps use OAuth with PKCE.

### API keys

Use `Authorization: Bearer <api-key>` (recommended) or `?api_key=<api-key>`. Keys grant `<resource>:<action>` scopes; a missing scope receives `403 MISSING_SCOPE`. Wildcard grants: `*`, `*:*`, `*:read`, `*:write`, and `<resource>:*`. `write` includes `read`. Write scopes also allow the DELETE operations documented on their endpoints; the consent screen lists them.

### OAuth 2.0

OAuth uses the authorization-code flow with mandatory PKCE (S256). No client-credentials, password or implicit grant is supported.

| Endpoint | URL |
|---|---|
| Issuer (`iss`) | `https://api.searchsoftware.nl` |
| Authorization | `https://v4.searchsoftware.nl/oauth/authorize` |
| Token / refresh | `https://api.searchsoftware.nl/oauth/token` |
| Revocation | `https://api.searchsoftware.nl/oauth/revoke` |
| Server metadata | `https://api.searchsoftware.nl/.well-known/oauth-authorization-server` |
| Protected-resource metadata | `https://api.searchsoftware.nl/.well-known/oauth-protected-resource/v4` |

1. Register a **Workspace app** in Settings → OAuth Apps as a workspace manager. It works only in that workspace. For a **Platform app** available to every workspace, contact SearchSoftware support. Managers can block platform apps within their workspace. Secret rotation defaults to a 24-hour overlap; select immediate rotation for a suspected leak or revoke the previous secret early.
2. Choose **Server app** if you can keep a secret, or **Native / desktop app** without a secret. Register exact redirect URIs. Server apps use HTTPS (HTTP localhost is allowed for development). Native apps can use HTTPS, a reverse-domain scheme such as `com.example.app:/callback`, or HTTP loopback IPs `127.0.0.1` / `[::1]` with any port. Native apps cannot use `localhost`.
3. Generate a random `code_verifier` of 43–128 characters from `A-Z a-z 0-9 - . _ ~`; calculate `code_challenge = BASE64URL(SHA256(code_verifier))` without padding. Store a random `state` with the verifier.
4. Send the user to this URL, URL-encoding each parameter:

```text
https://v4.searchsoftware.nl/oauth/authorize?response_type=code&client_id=CLIENT_ID&redirect_uri=REDIRECT_URI&scope=categories:read&state=RANDOM_STATE&code_challenge=CHALLENGE&code_challenge_method=S256
```

Check the callback `state` and `iss` (must equal `https://api.searchsoftware.nl`); read `code` or `error` and `error_description`. Codes last **60 seconds** and work once. Consent is remembered for server apps and HTTPS redirects. Native loopback and custom-scheme redirects always require consent.

5. Exchange the code. Placeholders represent your `pfc_…` client ID, `pfcs_…` secret, `pfac_…` code and `pfrt_…` refresh token. Server app (HTTP Basic):

```bash
curl -u "$CLIENT_ID:$CLIENT_SECRET" 'https://api.searchsoftware.nl/oauth/token' \
  --data-urlencode 'grant_type=authorization_code' \
  --data-urlencode "code=$CODE" --data-urlencode "redirect_uri=$REDIRECT_URI" \
  --data-urlencode "code_verifier=$CODE_VERIFIER"
```

Native app (no secret):

```bash
curl 'https://api.searchsoftware.nl/oauth/token' --data-urlencode 'grant_type=authorization_code' \
  --data-urlencode "client_id=$CLIENT_ID" --data-urlencode "code=$CODE" \
  --data-urlencode "redirect_uri=$REDIRECT_URI" --data-urlencode "code_verifier=$CODE_VERIFIER"
```

```json
{"access_token":"pfat_…","token_type":"Bearer","expires_in":3600,"refresh_token":"pfrt_…","scope":"categories:read"}
```

6. Call `GET /v4/me` (no scope required) to identify the user and workspace. Store tokens by **workspace and user**: user IDs are local to a workspace.

```bash
curl 'https://api.searchsoftware.nl/v4/me' -H "Authorization: Bearer $ACCESS_TOKEN"
```

```json
{"status":"ok","data":{"auth_type":"oauth","workspace":"example","user":{"id":"opaque-id","name":"alice","full_name":"Alice"},"scopes":["categories:read"]}}
```

7. Call resources with `Authorization: Bearer <access_token>`. OAuth tokens in query strings are refused with `400 INVALID_REQUEST`.
8. Refresh before or after access-token expiry:

```bash
curl 'https://api.searchsoftware.nl/oauth/token' --data-urlencode 'grant_type=refresh_token' \
  --data-urlencode "client_id=$CLIENT_ID" --data-urlencode "refresh_token=$REFRESH_TOKEN"
```

Server apps also authenticate using `-u "$CLIENT_ID:$CLIENT_SECRET"`. Save the new refresh token and discard the old one. Refresh from one place, behind a lock: if refreshed twice in parallel only the later pair stays valid. A lost response can be retried with the old token within **60 seconds**, while its replacement is unused; the lost pair is cancelled. Later reuse, or reuse after the replacement was used, revokes the token family. A narrower optional `scope` narrows the access token, while the refresh token retains its original scope.
9. Revoke when disconnecting (server apps also use HTTP Basic):

```bash
curl 'https://api.searchsoftware.nl/oauth/revoke' --data-urlencode "client_id=$CLIENT_ID" \
  --data-urlencode "token=$REFRESH_TOKEN" --data-urlencode 'token_type_hint=refresh_token'
```

Revoking a refresh token revokes its whole token family.

#### Browser extensions and native apps

Register as Native / desktop app, without a secret. For Chrome Manifest V3, register the exact result of `chrome.identity.getRedirectURL()`: `https://<extension-id>.chromiumapp.org/`. Keep the extension ID stable using the manifest `key`. Start with `chrome.identity.launchWebAuthFlow({url: authorizationURL, interactive: true})`; validate `state` and `iss` on the returned URL. Pikaflow and SearchSoftware have different issuers: an extension that supports both treats them as two authorization servers and compares `iss` with the issuer of the brand it started the sign-in with. With remembered consent and a signed-in user, `interactive: false` can obtain a new code silently.

Exchange codes, refresh and call the API from the **service worker** with `host_permissions` for `https://api.searchsoftware.nl/*`; there are no API CORS headers. Content scripts message the worker. Serialize refreshes, persist the replacement token before use, and retry a lost response within 60 seconds. Store tokens in `chrome.storage.local`, never in page-reachable storage.

#### Lifetimes

| Item | Lifetime |
|---|---|
| Authorization code | 60 seconds, single use |
| Access token | 1 hour |
| Refresh token | 30 days, renewed on refresh |
| Lost refresh response retry | 60 seconds, replacement still unused |

#### Scopes

OAuth accepts concrete scopes only, without wildcards. `write` includes `read`. `records:read` and `records:write` cover every custom record type, but not people, companies or jobs. Write scopes also allow the DELETE operations documented on their endpoints; the consent screen lists them. Workspace apps can request custom-record scopes such as `invoices:read` / `invoices:write`; platform apps use core scopes only. `users:write` is granted only to workspace managers. The token response reports the actual granted `scope`. OAuth tokens act as the authorizing user: records that user cannot open in the app (hidden or above their access level) behave as if they do not exist, together with their notes, comments, emails, files, work history and todo writes. Users who are not managers can delete only todos they created.

| Scope | Grants |
|---|---|
| `people:read` | Read people |
| `people:write` | Create / update people |
| `companies:read` | Read companies |
| `companies:write` | Create / update companies |
| `jobs:read` | Read jobs |
| `jobs:write` | Create / update jobs |
| `files:read` | Download files (also needs the record's read scope) |
| `files:write` | Reserved for future file write access |
| `sources:read` | Read sources |
| `sources:write` | Create / update sources and source groups |
| `workflows:read` | Read workflow phases and stages |
| `workflows:write` | Set workflow phase on a record |
| `users:read` | Read users |
| `users:write` | Create / update users |
| `lists:read` | Read lists |
| `lists:write` | Create / update lists and add / remove their members |
| `communications:read` | Read communications and methods |
| `communications:write` | Create communications and methods |
| `comments:read` | Read comments |
| `comments:write` | Create comments |
| `notes:read` | Read notes |
| `notes:write` | Create notes |
| `categories:read` | Read categories and category groups |
| `categories:write` | Create / update categories and category groups |
| `work_history:read` | Read work history entries |
| `work_history:write` | Create / update work history entries |
| `todos:read` | Read todos, todo boards, board lists, and labels |
| `todos:write` | Create / update todos, todo boards, board lists, and labels |
| `records:read` | Read all custom record types |
| `records:write` | Create / update all custom record types |

#### Acting as the user

OAuth defaults `created_by`, `completed_by` and comment `user_id` to the authorizing user. Setting a different actor returns `403 ACTOR_NOT_ALLOWED`. Use `/v4/me` to identify the actor.

#### When access ends and errors

Users can disconnect apps in Settings → Connected apps. Managers can disconnect users, delete workspace apps or block platform apps. Password changes and deactivation revoke grants; restriction blocks token use. Tokens stop working within 5 seconds and refresh fails with `invalid_grant`. Ask the user to authorize again when eligible.

Token endpoints return `429` with `Retry-After` when a client exceeds 120 token requests per minute.

Token endpoint errors include `invalid_client`, `invalid_grant`, `invalid_scope`, `invalid_request`, and `unsupported_grant_type`. Resource errors include `401 INVALID_TOKEN` (expired or revoked token), `403 MISSING_SCOPE` (missing scope), `403 ACTOR_NOT_ALLOWED` (different actor), `400 INVALID_REQUEST` (query-string token), `429 RATE_LIMITED` (too many invalid bearer tokens), and `503 TEMPORARILY_UNAVAILABLE` with `Retry-After: 5` (authentication unavailable).

## IDs

All record identifiers are opaque strings. Treat them as opaque tokens.

## Response envelope

Success:

```json
{ "status": "ok", "data": <payload> }
```

Error:

```json
{ "status": "error", "error": { "code": "INVALID_ID", "status_code": 400, "message": "Invalid id" } }
```

Common error codes: `INVALID_ID`, `INVALID_BODY`, `INVALID_API_KEY`, `INVALID_TOKEN`, `MISSING_SCOPE`, `ACTOR_NOT_ALLOWED`, `FORBIDDEN`, `NOT_FOUND`, `VALIDATION_FAILED`, `RATE_LIMITED`, `TEMPORARILY_UNAVAILABLE`, `INTERNAL_ERROR`.

Status defaults in examples: `400 INVALID_ID/INVALID_BODY`, `401 INVALID_API_KEY`, `403 FORBIDDEN`, `404 NOT_FOUND`, `422 VALIDATION_FAILED`, `429 RATE_LIMITED`, `500 INTERNAL_ERROR`, `503 TEMPORARILY_UNAVAILABLE`.

## `?include=`

`GET` endpoints accept an `include` query parameter — a comma-separated list of related objects to embed under `data.objects`.

| Resource | Available includes |
|----------|--------------------|
| people | `primary_email`, `primary_phone`, `primary_location`, `source`, `notes`, `flow` |
| companies | `primary_email`, `primary_phone`, `primary_location`, `source`, `notes`, `flow` |
| jobs | `primary_location`, `source`, `notes`, `flow`, `company` |

Unknown includes are silently ignored.

`flow` embeds the record's workflow flow status (its `phase_status_id` references the workflow phase objects from the workflows endpoints). `company` (jobs only) embeds the job's company (`name`, `image_url`, timestamps).

Single-object includes (`primary_email`, `primary_phone`, `primary_location`, `source`) use a JSON:API-style envelope:

```json
{ "id": "abc123", "type": "source", "attributes": { "name": "LinkedIn" } }
```

`notes` is different from other includes: it returns an array (up to 25 items), newest-first, instead of a single object or `null`.

## `?values=true`

Record list, get, and get-many endpoints accept `values=true` to add a per-item `values` object. `values` is keyed by the item's own `attributes` fields and hydrates reference ids/tokens into public object summaries. This is separate from `include` and does not add a top-level `objects` map.

## Versioning

The current major version is **v4**, mounted at `/v4`.

Version: 4.0.0

Authentication:
- bearer_auth: http (bearer). API key or OAuth access token in Authorization header
- oauth2_auth: oauth2. OAuth authorization code flow with mandatory S256 PKCE.
- apikey_auth: apiKey, query api_key. API key in query string
