# The Vigil API

> Technical reference for the Vigil GraphQL API at /api/graphql. It can do what the console does, uses personal access tokens and follows your roles.

Source: https://vigilsecure.net/docs/developers/api/

Vigil has one API, and the console, the guard app and Vigil's own agents all use it. It is
a GraphQL API, so a client asks for exactly the fields it needs, and the API describes
itself to any GraphQL tool.

Use it to connect Vigil to other systems or to pull data into your own reports. This page is
written for developers. If you would rather describe what you want in plain language, see
[Use Vigil with AI](https://vigilsecure.net/docs/setup/use-vigil-with-ai.md).

## Endpoint

|                      |                                                                                  |
| -------------------- | -------------------------------------------------------------------------------- |
| **GraphQL endpoint** | `/api/graphql`, on the same host as your Vigil console                           |
| **Requests**         | `POST` with a JSON body of `query` and `variables`                               |
| **Live updates**     | GraphQL subscriptions over WebSocket on the same path                            |
| **Schema**           | Introspection is enabled. Opening `/api/graphql` in a browser shows an explorer. |
| **File transfers**   | REST operations for uploads and downloads, described at `/api/docs`              |

## Authentication

Create a personal access token in the console under **Account settings → API tokens**.
[Create an access token](https://vigilsecure.net/docs/setup/use-vigil-with-ai.md#step-1-create-an-access-token) has the steps. Send it in the `Authorization` header,
over HTTPS:

```http
Authorization: Bearer vigil_pat_…
```

- A token is `vigil_pat_` followed by 64 letters and digits. Vigil shows it once.
- A token acts as the person who created it, in every organization they belong to. It has
  no scopes of its own.
- A token cannot create, list or revoke tokens. Only a signed-in person can.
- For an integration that should only read, or only work in one organization, create a
  separate Vigil account for it, invite that account with a limited role and create the
  token there.

## Permissions

A request can do exactly what its token's owner can do:

- The owner's [roles](https://vigilsecure.net/docs/concepts/roles.md) in an organization decide what the request can
  read and change there.
- A change to those roles applies from the next request.
- Chats are visible only to their participants, through the API as in the console.

A field you may not read comes back as `null` or with a `FORBIDDEN` error.

## How the API is organised

**Queries** start from a few roots:

| Root                                | What it holds                                                                       |
| ----------------------------------- | ----------------------------------------------------------------------------------- |
| `identity`                          | The caller: who the token belongs to                                                |
| `directory { organization(id: …) }` | One organization's data: sites, beacons, devices, members, agents, roles, artifacts |
| `communication`                     | Chats and messages                                                                  |
| `infrastructure`                    | Single devices, beacons and cameras by ID                                           |

**Mutations** are grouped by area, for example `communication { message { post(…) } }` to
post a message and `infrastructure { device { command(…) } }` to send a device command.

Do not guess field names. Ask the schema, which is always current:

```sh
curl "$VIGIL_API_URL" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $VIGIL_API_TOKEN" \
  --data '{"query":"{ __schema { queryType { fields { name description } } } }"}'
```

### Example: list your organizations' sites

```graphql
query ($org: UUID!) {
  directory {
    organization(id: $org) {
      name
      sites {
        items {
          id
          name
          latitude
          longitude
        }
      }
    }
  }
}
```

[Make your first API request](https://vigilsecure.net/docs/developers/make-your-first-api-request.md) shows how to send a
query with curl or TypeScript.

## Errors

A GraphQL response can carry errors with HTTP status 200. Check the `errors` array of every
response as well as the status.

| Code              | Meaning                                                                   |
| ----------------- | ------------------------------------------------------------------------- |
| `UNAUTHENTICATED` | The token is missing, wrong, expired or revoked                           |
| `FORBIDDEN`       | The token's owner lacks the permission for this field                     |
| `NOT_FOUND`       | The record does not exist, is archived or belongs to another organization |
| `BAD_USER_INPUT`  | The input is invalid; the message says what to fix                        |

## Token expiry and rotation

Tokens expire after 90 days unless you choose another period, up to 365 days. Expiry and
revocation are checked on every request. The **API tokens** page lists each token with its
state, the first characters of its value, its expiry date and when it was last used.

To rotate a token, create a new one, switch your integration over and check that it works,
then choose **Revoke** on the old one. Revoking takes effect straight away and cannot be
undone. Signing out of the console does not revoke tokens.

## Posting from other systems without a token

To let another system post messages into one chat, use a
[webhook](https://vigilsecure.net/docs/developers/webhooks.md). It needs no token and can do nothing else.

## Troubleshooting

- **`identity` is `null`.** Check the header, the token's expiry and that you are calling
  the same Vigil host the token was created on.
- **Permission errors.** Check the token owner's roles in that organization.
- **A browser client acts as the wrong person.** A signed-in console session in the same
  browser takes precedence over a token. Call the API from a server or a script.
- **Never log token values.**

## Related

- [Make your first API request](https://vigilsecure.net/docs/developers/make-your-first-api-request.md)
- [Post into a chat with a webhook](https://vigilsecure.net/docs/developers/webhooks.md)
- [Use Vigil with AI](https://vigilsecure.net/docs/setup/use-vigil-with-ai.md)
