# Create a session

Create an ID Check session for one user, and get the link that starts it in the BankID app.

```http
POST https://handover.prod.idcheck.bankid.no/api/v1/session
```

See the [Handover API spec](https://docs.stoe.no/id-check/openapi.md) for full request and response schemas.

## How it fits

- Each request creates a new session with a new `id`.
- Give the user the `intent_url` from the response. On a phone, show it as a link. On a computer, show it as a QR code that the user scans with their phone.
- If nobody opens the session, it expires after 5 minutes.
- To show the user the progress of the session, [fetch the session](https://docs.stoe.no/id-check/fetch-session.md) until its state is final. Never base a decision on the state.
- Someone other than the expected customer can complete the session. Check the results as [Check the results](https://docs.stoe.no/id-check.md#check-the-results) describes.

For the full flow, see the [process overview](https://docs.stoe.no/id-check.md#process-overview).

## Request

Send an access token with the `handover:create` scope. See [Authentication](https://docs.stoe.no/id-check/authentication.md).

| Parameter              | Type   | Required | Description               |
| ---------------------- | ------ | -------- | ------------------------- |
| `Authorization` header | string | Yes      | `Bearer <<ACCESS_TOKEN>>` |
| `Content-Type` header  | string | Yes      | `application/json`        |

Send the body as JSON, in this envelope:

| Parameter    | Type   | Required | Description                                                                               |
| ------------ | ------ | -------- | ----------------------------------------------------------------------------------------- |
| `request_id` | string | Yes      | A unique ID for the request, such as a UUID. The Handover API returns it in the response. |
| `timestamp`  | string | Yes      | The time of the request, in UTC, in ISO 8601 format.                                      |
| `payload`    | object | Yes      | The values of the session, as in the next table.                                          |

Put these values in `payload`:

| Parameter          | Type   | Required | Description                                                                                                                                                                             |
| ------------------ | ------ | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `client_reference` | string | Yes      | The merchant's own reference for the session. See [Client reference](https://docs.stoe.no/id-check/create-session.md#client-reference).                                                 |
| `reason`           | string | Yes      | `REIDENTIFICATION` or `TESTING`. New values may be added in the future and may influence what users see in the BankID app when verifying their identity.                                |
| `success_url`      | string | Yes      | The URL that the BankID app opens when the user completes ID Check. See [Success and failure URLs](https://docs.stoe.no/id-check/create-session.md#success-and-failure-urls).           |
| `failure_url`      | string | Yes      | The URL that the BankID app opens when ID Check fails or the user cancels it. See [Success and failure URLs](https://docs.stoe.no/id-check/create-session.md#success-and-failure-urls). |

If a value is missing or not valid, the response is `400 Bad Request`. The message names the value, for example `Invalid property: success_url`.

```http
POST /api/v1/session HTTP/1.1
Host: handover.prod.idcheck.bankid.no
Authorization: Bearer <<ACCESS_TOKEN>>
Content-Type: application/json

{
  "request_id": "75a1f89f-3569-4bd1-81ca-ae2ad173d1b2",
  "timestamp": "2021-06-22T11:53:27.689Z",
  "payload": {
    "client_reference": "2e496a55-3b51-4180-be71-bfd396043065",
    "reason": "REIDENTIFICATION",
    "success_url": "https://example.com/id-check/success",
    "failure_url": "https://example.com/id-check/failure"
  }
}
```

### Client reference

- Use the client reference to match the session and its results to the expected customer in the merchant's system.
- Send 1 to 64 characters. Use only `A`–`Z`, `a`–`z`, `0`–`9`, `.`, `_`, `:`, `~` and `-`. A UUID meets these rules.
- Use a new value for each session. The Handover API does not check this.
- Do not put personal data in the client reference, such as a national identity number, a user ID or an email address.
- The Handover API returns the client reference with the session, and ID Check puts it in the JSON file of the [results](https://docs.stoe.no/id-check.md#receive-the-results).

### Success and failure URLs

- Send absolute URLs, such as `https://example.com/id-check/success`.
- The BankID app opens the URL on the user's phone when the session ends in the app. This also applies when the user started on a computer. The pages at these URLs must work on a phone.
- The Handover API adds the session ID to both URLs as the query parameter `id`. If a URL already has an `id` parameter, the Handover API replaces its value.
- The BankID app adds the query parameter `code` to the failure URL. See [Redirect codes](https://docs.stoe.no/id-check/create-session.md#redirect-codes).

This is an example of a failure URL that the BankID app opens:

```text
https://example.com/id-check/failure?id=3fa85f64-5717-4562-b3fc-2c963f66afa6&code=USER_CANCELLED
```

> **A redirect is not proof of success**
>
> A redirect to the success URL does not prove that the user completed ID Check. Anyone can open the URL. The state of the session is not proof either. Base each decision on the results, after the checks in [Check the results](https://docs.stoe.no/id-check.md#check-the-results).

### Redirect codes

The `code` parameter on the failure URL can have these values:

| Code              | Meaning                                                                                                                    |
| ----------------- | -------------------------------------------------------------------------------------------------------------------------- |
| `BAD_DOCUMENT`    | The app could not accept the identity document. For example, the document has expired, or the app could not read its chip. |
| `BAD_PERSON`      | The face scan failed.                                                                                                      |
| `CLIENT_FAILURE`  | An error occurred in the BankID app. For example, the phone cannot read near-field communication (NFC) chips.              |
| `SERVER_FAILURE`  | A server returned an error to the BankID app.                                                                              |
| `SESSION_TIMEOUT` | The session expired, or it was not available.                                                                              |
| `USER_CANCELLED`  | The user cancelled ID Check.                                                                                               |

The iOS app and the Android app can send different codes for the same problem.

These codes are not the same as the `code` values of a session. For those, see [Failure codes](https://docs.stoe.no/id-check/create-session.md#failure-codes).

> **Handle new codes**
>
> The BankID app can add new codes and new query parameters. Handle an unknown code as a failure with no known reason. Ignore query parameters that the integration does not use.

## Response

A successful request returns `202 Accepted` and the new session in `payload`.

```json
{
  "payload": {
    "id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
    "branch_id": "e2c236a3-01f2-44a6-bb2a-1350938d84e4",
    "human_id": "not-used-1234",
    "client_reference": "2e496a55-3b51-4180-be71-bfd396043065",
    "state": "created",
    "reason": "REIDENTIFICATION",
    "artefact_type": "PDF",
    "success_url": "https://example.com/id-check/success?id=3fa85f64-5717-4562-b3fc-2c963f66afa6",
    "failure_url": "https://example.com/id-check/failure?id=3fa85f64-5717-4562-b3fc-2c963f66afa6",
    "intent_url": "https://prod.idcheck.bankid.no/start?handoverId=3fa85f64-5717-4562-b3fc-2c963f66afa6",
    "last_update": "2021-06-22T11:53:28.150Z",
    "qr_svg_path": "<<SVG_PATH_DATA>>",
    "play_store_url": null,
    "apple_store_url": null
  },
  "request_id": "75a1f89f-3569-4bd1-81ca-ae2ad173d1b2",
  "response_id": "response:0b3f8c5e-2f4a-4f7e-9c1d-5a6b7c8d9e0f",
  "timestamp": "2021-06-22T11:53:28.153Z"
}
```

The envelope has these properties:

| Property      | Type   | Description                                                    |
| ------------- | ------ | -------------------------------------------------------------- |
| `payload`     | object | The session.                                                   |
| `request_id`  | string | The `request_id` from the request.                             |
| `response_id` | string | An ID for the response. Give it to Stø in each problem report. |
| `timestamp`   | string | The time of the response, in UTC, in ISO 8601 format.          |

The session has these properties:

| Property           | Type           | Description                                                                                                                                                        |
| ------------------ | -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `id`               | string         | The session ID, a UUID. Use it to [fetch the session](https://docs.stoe.no/id-check/fetch-session.md).                                                             |
| `branch_id`        | string         | The ID of the integration in ID Check. The BankID app uses it to show the name and logo of the merchant.                                                           |
| `human_id`         | string         | Deprecated. Ignore it.                                                                                                                                             |
| `client_reference` | string         | The client reference from the request.                                                                                                                             |
| `policy`           | string         | The name of the rules that ID Check uses to check the identity document. It is present only when the session has such rules. The integration can ignore it.        |
| `state`            | string         | The state of the session. See [Session states](https://docs.stoe.no/id-check/create-session.md#session-states).                                                    |
| `code`             | string         | The failure code. It is present only in some sessions with the state `failed`. See [Failure codes](https://docs.stoe.no/id-check/create-session.md#failure-codes). |
| `reason`           | string         | The reason from the request.                                                                                                                                       |
| `artefact_type`    | string         | Deprecated. Ignore it.                                                                                                                                             |
| `success_url`      | string         | The success URL from the request, with the session ID added as `id`.                                                                                               |
| `failure_url`      | string         | The failure URL from the request, with the session ID added as `id`.                                                                                               |
| `intent_url`       | string         | The link that starts the session in the BankID app. On a phone, show it as a link. On a computer, show it as a QR code.                                            |
| `last_update`      | string or null | The time of the last change to the session, in ISO 8601 format, or `null`.                                                                                         |
| `qr_svg_path`      | string         | Deprecated. SVG path data for a QR code of `intent_url`. Make a QR code from `intent_url` instead.                                                                 |
| `play_store_url`   | null           | Not used. The value is always `null`.                                                                                                                              |
| `apple_store_url`  | null           | Not used. The value is always `null`.                                                                                                                              |

Stø does not remove properties from version 1 of the API.

> **Handle new properties and states**
>
> Stø can add new properties and new states. Ignore properties that the integration does not use. Handle an unknown state as a state that is not final.

### Session states

| State              | Meaning                                                                | Final |
| ------------------ | ---------------------------------------------------------------------- | ----- |
| `created`          | The Handover API created the session. Nobody has opened it yet.        | No    |
| `started`          | The user opened the session in the BankID app.                         | No    |
| `mrz_completed`    | The app read the machine-readable zone (MRZ) of the identity document. | No    |
| `nfc_completed`    | The app read the chip in the identity document.                        | No    |
| `face_match_retry` | The face scan failed, and the user can try again.                      | No    |
| `completed`        | The user completed ID Check.                                           | Yes   |
| `failed`           | The session failed. `code` can give the reason.                        | Yes   |
| `aborted`          | The user cancelled ID Check in the app.                                | Yes   |
| `expired`          | Nobody opened the session in time.                                     | Yes   |

- A session moves forward through the states. It can skip states, so a poll does not always show each state.
- A session expires only if nobody opens it.
- If the user stops after opening the session, the state changes to `failed` with the code `ABANDONED`.
- A final state does not change.
- The state can change after the results arrive, or not at all. Use it only to show the user information.

### Failure codes

When the state is `failed`, `code` can have these values:

| Code             | Meaning                                                                                                                                      |
| ---------------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| `ABANDONED`      | The user did not finish. There was no progress for 15 minutes, or the session was not complete 1 hour after the user started it.             |
| `BAD_DOCUMENT`   | The identity document failed the checks.                                                                                                     |
| `BAD_PERSON`     | The face scan failed.                                                                                                                        |
| `CLIENT_FAILURE` | The BankID app could not continue. For example, ID Check could not verify the requests of the app, or the document reader returned an error. |
| `SERVER_FAILURE` | An error stopped the session.                                                                                                                |

A session with the state `aborted` has no code. A session with the state `failed` can have no code, or a code that is not in this table. Handle these as a failure with no known reason.

## Errors

Errors use the same envelope as a successful response. In `payload`, `code` identifies the error and `message` describes it. Use `code` in the integration logic, because the message can change.

| Status                      | `code`                | Cause                                                                                                     |
| --------------------------- | --------------------- | --------------------------------------------------------------------------------------------------------- |
| `400 Bad Request`           | `EINVALIDREQUEST`     | A value is missing or not valid, or the body has no `request_id` or `payload`.                            |
| `401 Unauthorized`          | `EUNAUTHORISED`       | The access token is not valid or has expired, or the API client is not set up correctly.                  |
| `403 Forbidden`             | `EFORBIDDEN`          | The request has no `Authorization` header, or the access token does not have the `handover:create` scope. |
| `406 Not Acceptable`        | `EINVALIDCONTENTTYPE` | The request has a `Content-Type` header with a value other than `application/json`.                       |
| `500 Internal Server Error` | `EUNKNOWN`            | An error occurred in the Handover API.                                                                    |
| `503 Service Unavailable`   | `EUNAVAILABLE`        | The service is not available. Wait for the time in the `Retry-After` header, then try again.              |

```json
{
  "payload": {
    "code": "EINVALIDREQUEST",
    "message": "Invalid property: success_url"
  },
  "request_id": "75a1f89f-3569-4bd1-81ca-ae2ad173d1b2",
  "response_id": "response:9d8c7b6a-5f4e-4d3c-8b2a-1f0e9d8c7b6a",
  "timestamp": "2021-06-22T11:53:28.153Z"
}
```

A request that does not return `202 Accepted` returns no session. To try again, send a new request.

## Next steps

- [Fetch a session](https://docs.stoe.no/id-check/fetch-session.md): read the state of the session, to show it to the user.
- [Receive the results](https://docs.stoe.no/id-check.md#receive-the-results): how ID Check sends the results of a completed session.
- [Check the results](https://docs.stoe.no/id-check.md#check-the-results): verify the results and compare them with the expected customer.
- [Handover API spec](https://docs.stoe.no/id-check/openapi.md): the full request and response schemas.
