# Manage physical keys with the Salto Space API
> How to list encoders and assign, update, and cancel a user's physical key at an encoder with the Salto Space API.


The Space API, part of Salto IDM Connect, is a REST API that communicates with your Salto Space installation over the SHIP protocol.
This guide explains how to manage a user's physical key, such as a card, through the API:

- List the encoders configured in Salto Space.
- Assign a new key to a user at an encoder.
- Update a user's key with their latest access.
- Cancel a user's key.

You can use any HTTP client, such as Postman, or your own application.
To assign or update a key, present the card at the encoder while the request runs.

{{% info-panel %}}
Before you start, follow the guide to [connect Salto Space to the Space API](https://support.saltosystems.com/idm/connect/space-api/connect-salto-space/).
That guide explains how Salto connects the Space API to your Salto Space installation and issues the API credentials you need.
Then [request an access token](/space/space-api/authentication) for the requests shown here.
The user must already exist in Salto Space with the access you want to write to the key.
See [Manage users](/space/space-api/manage-users).
{{% /info-panel %}}

## Step 1: Find the encoder {#step-1-find-the-encoder}

1. Use the `GET /api/v1/encoders` endpoint to list the encoders configured in Salto Space.
To limit the number of results, add the `maxRows` query parameter.
2. Copy the `EncoderId` of the encoder where the card is presented, as the response shows it, including any spaces.
Each encoder in the response also has a `Type` and an `IpAddress`:

    ```json
    {
      "Data": [
        {
          "EncoderId": "ENCODER_ID",
          "Type": "1",
          "IpAddress": "0.0.0.0"
        }
      ],
      "Success": true,
      "Message": "OK"
    }
    ```

## Step 2: Assign a new key at the encoder {#step-2-assign-a-new-key-at-the-encoder}

1. Use the `POST /api/v1/keys/AssignNewKey` endpoint to assign a new key to a user.
The following payload shows the fields:

    ```json
    {
      "extUserId": "EXTERNAL_USER_ID",
      "encoderId": "ENCODER_ID",
      "timeoutSeconds": 30,
      "returnRomCode": true,
      "skipKeyRemoval": false
    }
    ```

    - `extUserId` is the external ID of the user who receives the key.
    - `encoderId` is the ID of the encoder you copied in Step 1.
    - `timeoutSeconds` is how long the encoder waits for a card before the operation times out.
    - `returnRomCode` returns the ROM code of the card in the response when set to `true`.
    - `skipKeyRemoval` skips the key-removal step at the end of the operation when set to `true`.

2. Present the card at the encoder before the timeout ends.
The encoder writes the user's current access to the card, and the API responds when the card is written.
When you set `returnRomCode` to `true`, the response contains the ROM code of the card:

    ```json
    {
      "Data": {
        "ExtUserId": "EXTERNAL_USER_ID",
        "RomCode": "ROM_CODE"
      },
      "Success": true,
      "Message": "OK"
    }
    ```

3. Confirm that the user has the key.
When you get the user with the `GET /api/v1/users/ID` endpoint, `CurrentRomCode` contains the ROM code of the card.
In Salto Space, the user page shows the key status bar with the **Update key** and **Cancel key** buttons.

    ![User page in Salto Space showing the key status bar after the key is assigned.](images/physical-key-assigned.png)
    {.border}

## Step 3: Update the key with the latest access {#step-3-update-the-key-with-the-latest-access}

When a user's access changes, for example after you assign a new user access level, rewrite their key so it carries the latest access.

1. Use the `POST /api/v1/keys/UpdateCurrentKey` endpoint with the same payload as in Step 2.
2. Present the user's card at the encoder before the timeout ends.
The encoder rewrites the card with the user's latest access.
The response has the same format as in Step 2, and `RomCode` is `null` when you set `returnRomCode` to `false`.

## Step 4: Cancel the key {#step-4-cancel-the-key}

1. Use the `POST /api/v1/keys/CancelKey` endpoint, with the user's external ID in the `extUserId` query parameter, to cancel the user's key.
This request has no body, and the response contains only the `Success` flag and the `Message`.
2. After a successful response, the key can no longer open any access point.
When you get the user, `CurrentRomCode` is `null`.
In Salto Space, the user page shows the **Assign key** button instead of the key status bar.

    ![User page in Salto Space showing the Assign key button after the key is canceled.](images/physical-key-canceled.png)
    {.border}

This request cancels the user's key whether it is a physical or a mobile key.
It is the same operation as the `PUT /api/v1/MobileKeys/CancelUserKey` endpoint in [Manage JustIN Mobile keys](/space/space-api/manage-justin-mobile-keys#step-4-cancel-the-mobile-key-of-the-user).

## Handling responses {#handling-responses}

Every endpoint returns HTTP `200`, including when the operation does not succeed.
The response body carries a `Success` flag and a `Message`, so always check the `Success` flag rather than the HTTP status code to determine the outcome.

For example, if the `encoderId` doesn't match an encoder in Salto Space, the API returns HTTP `200` with `Success` set to `false`:

```json
{
  "Data": null,
  "Success": false,
  "Message": "One or more errors occurred. (Ship error 400: Unknown peripheral)"
}
```

## API definition and documentation {#api-definition-and-documentation}

See the [Space API reference](https://space.api.saltoidm.app/) for a full description of the endpoints and entities available through the API.

The Space API uses a limited set of the functionality available in the full SHIP protocol.
Contact `support-id@saltosystems.com` if you need functionality in the API that is not yet available.

