# Authenticate to the local HTTP API

> Find the Sonjoo API port and token, use the X-Sonjoo-Token header, and diagnose local authentication errors.

Canonical documentation: <https://sajaddehshiri.ir/sonjoo-seo-audit/api/authentication/>

Sonjoo serves the local HTTP API at `http://127.0.0.1:<port>`. Send the running server's token in the `X-Sonjoo-Token` header on every scripted API request, including `GET /api` and `GET /api/info`.

## Where to find the port and token

Sonjoo writes `serve.json` to its application home after it binds the port.

| Setting                  | Connection file                                                                                                                                                                 |
| ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `SONJOO_HOME` is set     | `$SONJOO_HOME/serve.json`                                                                                                                                                       |
| Default application home | `${HOME}/.sonjoo/serve.json`, or `${USERPROFILE}/.sonjoo/serve.json` if `HOME` is unset. If neither is set, Sonjoo uses `.sonjoo/serve.json` relative to its working directory. |

The file contains `port`, `token`, `pid`, and `startedAt`. The default CLI port is `7360`. Pass `sonjoo serve --no-open --port 0` to request a free port and use the actual value from the file.

Sonjoo generates a token each time the server starts. Reload the connection file after a restart and use the same `SONJOO_HOME` as the running server. The file is restricted to the current user on Unix and removed on graceful shutdown. Keep its contents out of source control, logs, and shared reports.

## Send a local request

[Run the quickstart](https://sajaddehshiri.ir/sonjoo-seo-audit/api/quickstart/) to see scripts that pass the token at runtime. Browser dashboard sessions use a local session cookie. Scripts can use the header directly.

Sonjoo validates the request host and origin. Run API integrations locally in a shell or a Node.js process. A documentation page hosted on GitHub Pages cannot make cross-origin requests to the local Sonjoo API.

## Diagnose a rejected request

| Symptom                            | Check                                                                                               |
| ---------------------------------- | --------------------------------------------------------------------------------------------------- |
| File missing or connection refused | Start Sonjoo and check the script uses the same application home as the server.                     |
| HTTP `401`                         | Read the latest `serve.json` and check that the token header is set.                                |
| HTTP `403`                         | Check that the request is local and uses the loopback host and port. Run the example in a terminal. |

See the [API error reference](https://sajaddehshiri.ir/sonjoo-seo-audit/api/reference/#errors) for other response codes.
