# Manually Register a Device with the REST API

For quick development, you can use the [registration tool](https://developers.google.com/assistant/sdk/reference/device-registration/device-tool)
provided with the Google Assistant SDK to quickly register or update a device
model and instance.

If you don't want to use this tool, you can manually register
or update a device using a JSON file and the REST API. You can do this on any
computer. First [get an access token](https://developers.google.com/assistant/sdk/reference/device-registration/register-device-manual#get-access-token) and then skip to the
appropriate section.

> [!NOTE]
> **Note:** The commands below assume you are working in a [Python virtual environment](https://docs.python.org/3/library/venv.html).

## Get an access token

1. Make sure the authorization tool is up to date:

   ```
   python -m pip install --upgrade google-auth-oauthlib[tool]
   ```

   <br />

2. Obtain credentials to be able to register a new device model. Reference the JSON file you copied over to the
   device in a previous

   [step](https://developers.google.com/assistant/sdk/guides/configure-developer-project).

   ```
   google-oauthlib-tool --scope https://www.googleapis.com/auth/assistant-sdk-prototype \
         --headless --client-secrets /path/to/client_secret_client-id.json
   ```

   You should see a URL displayed in the terminal:

   ```
   Please visit this URL to authorize this application: https://...
   ```

   <br />

3. Copy the URL and paste it into a browser (this can be done on your
   development machine, or any other machine). After you approve, a code
   will appear in your browser, such as "4/XXXX". Copy and paste this code into
   the terminal:

   <br />

   ```
   Enter the authorization code:
   ```

   <br />

   If authorization was successful, you will see a JSON response similar to
   the following:

   ```
   {
      "scopes": ["https://www.googleapis.com/auth/assistant-sdk-prototype"],
      "token_uri": "https://accounts.google.com/o/oauth2/token",
      "token": "ya29.GlujBLa_kuXZ5GnGBPBe_A6NpczLcpOtglEC0wHVORnmEhHETzlSW",
      "client_id": "795595571889-6iesr9a3nkmnipbdfnqi6gehiklm2m28.apps.googleusercontent.com",
      "client_secret": "Un8_TNFnb55555auSAGasvAg",
      "refresh_token": "1/4ZMBhTR3bTYWVEMatYWLOxW755555hlQXZI5uC02F2U"
    }
   ```

   <br />

   If instead you see `UNAUTHENTICATED`, then an invalid code was entered.
   Try again, taking care to copy and paste the entire code.
4. Find `token` in the JSON response. Copy this access token (not including the
   double quotes) into an environment variable:

   ```
   ACCESSTOKEN=access-token
   ```

## Device model operations

### Define and register the device model

1. Create a file (e.g., `device_model.json`) describing the characteristics
   of your device model. See the device model JSON [reference](https://developers.google.com/assistant/sdk/reference/device-registration/model-and-instance-schemas.html#device_model_json)
   for more information.

2. Register your device model using either of the following methods. Remember to
   substitute the [`project_id`](https://developers.google.com/assistant/sdk/reference/device-registration/model-and-instance-schemas.html#device_model_json)
   associated with the device model in the URLs below.

   - Use the following command:

     ```
     curl -s -X POST -H "Content-Type: application/json" \
     -H "Authorization: Bearer $ACCESSTOKEN" -d @device_model.json \
     https://embeddedassistant.googleapis.com/v1alpha2/projects/project_id/deviceModels/
     ```
   - Send an HTTPS request with the following attributes:

     - URL: [https://embeddedassistant.googleapis.com/v1alpha2/projects/\<project_id\>/deviceModels/](https://embeddedassistant.googleapis.com/v1alpha2/projects/&lt;project_id&gt;/deviceModels/)
     - Method: `POST`
     - Body: Contents of `device_model.json`
     - Headers: `Authorization: Bearer <access-token>` where \<access-token\> is the string containing the [access token](https://developers.google.com/assistant/sdk/reference/device-registration/register-device-manual#get-access-token)

   For either method, the server should return a copy of the device model JSON
   that you sent. If the model already exists on the server, you will receive
   an `ALREADY_EXISTS` error.

### Get a device model

Get a device model using either of the following methods. Remember to
substitute the [`project_id`](https://developers.google.com/assistant/sdk/reference/device-registration/model-and-instance-schemas.html#device_model_json)
and [`device_model_id`](https://developers.google.com/assistant/sdk/reference/device-registration/model-and-instance-schemas.html#device_model_json)
associated with your device model in the URLs below.

- Use the following command:

  ```
  curl -s -X GET -H "Content-Type: application/json" \
  -H "Authorization: Bearer $ACCESSTOKEN" \
  https://embeddedassistant.googleapis.com/v1alpha2/projects/project_id/deviceModels/device_model_id
  ```
- Send an HTTPS request with the following attributes:

  - URL: [https://embeddedassistant.googleapis.com/v1alpha2/projects/\<project_id\>/deviceModels/\<device_model_id\>](https://embeddedassistant.googleapis.com/v1alpha2/projects/&lt;project_id&gt;/deviceModels/&lt;device_model_id&gt;)
  - Method: `GET`
  - Body: None
  - Headers: `Authorization: Bearer <access-token>` where \<access-token\> is the string containing the [access token](https://developers.google.com/assistant/sdk/reference/device-registration/register-device-manual#get-access-token)

### List the device models

List all of your device models for a given project using either of the following methods. Remember to
substitute the [`project_id`](https://developers.google.com/assistant/sdk/reference/device-registration/model-and-instance-schemas.html#device_model_json)
associated with your device models in the URLs below.

- Use the following command:

  ```
  curl -s -X GET -H "Content-Type: application/json" \
  -H "Authorization: Bearer $ACCESSTOKEN" \
  https://embeddedassistant.googleapis.com/v1alpha2/projects/project_id/deviceModels/
  ```
- Send an HTTPS request with the following attributes:

  - URL: [https://embeddedassistant.googleapis.com/v1alpha2/projects/\<project_id\>/deviceModels/](https://embeddedassistant.googleapis.com/v1alpha2/projects/&lt;project_id&gt;/deviceModels/)
  - Method: `GET`
  - Body: None
  - Headers: `Authorization: Bearer <access-token>` where \<access-token\> is the string containing the [access token](https://developers.google.com/assistant/sdk/reference/device-registration/register-device-manual#get-access-token)

### Update the device model

Update the device model using either of the following methods. Remember to
substitute the [`project_id`](https://developers.google.com/assistant/sdk/reference/device-registration/model-and-instance-schemas.html#device_model_json)
and [`device_model_id`](https://developers.google.com/assistant/sdk/reference/device-registration/model-and-instance-schemas.html#device_model_json)
associated with your device model.

- Use the following command:

  ```
  curl -s -X PUT -H "Content-Type: application/json" \
  -H "Authorization: Bearer $ACCESSTOKEN" -d @device_model.json \
  https://embeddedassistant.googleapis.com/v1alpha2/projects/project_id/deviceModels/device_model_id
  ```
- Send an HTTPS request with the following attributes:

  - URL: [https://embeddedassistant.googleapis.com/v1alpha2/projects/\<project_id\>/deviceModels/\<device_model_id\>](https://embeddedassistant.googleapis.com/v1alpha2/projects/&lt;project_id&gt;/deviceModels/&lt;device_model_id&gt;)
  - Method: `PUT`
  - Body: Contents of `device_model.json`
  - Headers: `Authorization: Bearer <access-token>` where \<access-token\> is the string containing the [access token](https://developers.google.com/assistant/sdk/reference/device-registration/register-device-manual#get-access-token)

For either method, the server should return a copy of the updated device
model JSON that you sent.

### Delete a device model

Delete a device model using either of the following methods. Remember to
substitute the [`project_id`](https://developers.google.com/assistant/sdk/reference/device-registration/model-and-instance-schemas.html#device_model_json)
and [`device_model_id`](https://developers.google.com/assistant/sdk/reference/device-registration/model-and-instance-schemas.html#device_model_json)
associated with your device model in the URLs below.

- Use the following command:

  ```
  curl -s -X DELETE -H "Content-Type: application/json" \
  -H "Authorization: Bearer $ACCESSTOKEN" \
  https://embeddedassistant.googleapis.com/v1alpha2/projects/project_id/deviceModels/device_model_id
  ```
- Send an HTTPS request with the following attributes:

  - URL: [https://embeddedassistant.googleapis.com/v1alpha2/projects/\<project_id\>/deviceModels/\<device_model_id\>](https://embeddedassistant.googleapis.com/v1alpha2/projects/&lt;project_id&gt;/deviceModels/&lt;device_model_id&gt;)
  - Method: `DELETE`
  - Body: None
  - Headers: `Authorization: Bearer <access-token>` where \<access-token\> is the string containing the [access token](https://developers.google.com/assistant/sdk/reference/device-registration/register-device-manual#get-access-token)

## Device instance operations

### Define and register a device instance

1. Create a file (e.g., `test_device.json`) that identifies your device.
   See the device instance JSON [reference](https://developers.google.com/assistant/sdk/reference/device-registration/model-and-instance-schemas.html#device_instance_json)
   for more information.

2. Register your device using either of the following methods. Remember to
   substitute the [`project_id`](https://developers.google.com/assistant/sdk/reference/device-registration/model-and-instance-schemas.html#device_model_json)
   associated with the device model in the URLs below.

   - Use the following command:

     ```
     curl -s -X POST -H "Content-Type: application/json" \
     -H "Authorization: Bearer $ACCESSTOKEN" -d @test_device.json \
     https://embeddedassistant.googleapis.com/v1alpha2/projects/project_id/devices/
     ```
   - Send an HTTPS request with the following attributes:

     - URL: [https://embeddedassistant.googleapis.com/v1alpha2/projects/\<project_id\>/devices/](https://embeddedassistant.googleapis.com/v1alpha2/projects/&lt;project_id&gt;/devices/)
     - Method: `POST`
     - Body: Contents of `test_device.json`
     - Headers: `Authorization: Bearer <access-token>` where \<access-token\> is the string containing the [access token](https://developers.google.com/assistant/sdk/reference/device-registration/register-device-manual#get-access-token)

   For either method, the server should return a copy of the device instance JSON
   that you sent. If the instance already exists on the server, you will receive
   an `ALREADY_EXISTS` error.

### Get a device instance

Get your device instance using either of the following methods. Remember to
substitute the [`project_id`](https://developers.google.com/assistant/sdk/reference/device-registration/model-and-instance-schemas.html#device_model_json)
associated with the device model and the device [`id`](https://developers.google.com/assistant/sdk/reference/device-registration/model-and-instance-schemas.html#device_instance_json) in the URLs below.

- Use the following command:

  ```
  curl -s -X GET -H "Content-Type: application/json" \
  -H "Authorization: Bearer $ACCESSTOKEN" \
  https://embeddedassistant.googleapis.com/v1alpha2/projects/project_id/devices/id
  ```
- Send an HTTPS request with the following attributes:

  - URL: [https://embeddedassistant.googleapis.com/v1alpha2/projects/\<project_id\>/devices/\<id\>](https://embeddedassistant.googleapis.com/v1alpha2/projects/&lt;project_id&gt;/devices/&lt;id&gt;)
  - Method: `GET`
  - Body: None
  - Headers: `Authorization: Bearer <access-token>` where \<access-token\> is the string containing the [access token](https://developers.google.com/assistant/sdk/reference/device-registration/register-device-manual#get-access-token)

### List the device instances

List all of your device instances for a given project using either of the
following methods. Remember to substitute the [`project_id`](https://developers.google.com/assistant/sdk/reference/device-registration/model-and-instance-schemas.html#device_model_json)
associated with the device instances in the URLs below.

- Use the following command:

  ```
  curl -s -X GET -H "Content-Type: application/json" \
  -H "Authorization: Bearer $ACCESSTOKEN" \
  https://embeddedassistant.googleapis.com/v1alpha2/projects/project_id/devices/
  ```
- Send an HTTPS request with the following attributes:

  - URL: [https://embeddedassistant.googleapis.com/v1alpha2/projects/\<project_id\>/devices/](https://embeddedassistant.googleapis.com/v1alpha2/projects/&lt;project_id&gt;/devices/)
  - Method: `GET`
  - Body: None
  - Headers: `Authorization: Bearer <access-token>` where \<access-token\> is the string containing the [access token](https://developers.google.com/assistant/sdk/reference/device-registration/register-device-manual#get-access-token)

### Delete a device instance

Delete your device instance using either of the following methods. Remember to
substitute the [`project_id`](https://developers.google.com/assistant/sdk/reference/device-registration/model-and-instance-schemas.html#device_model_json)
associated with the device model and the device [`id`](https://developers.google.com/assistant/sdk/reference/device-registration/model-and-instance-schemas.html#device_instance_json) in the URLs below.

- Use the following command:

  ```
  curl -s -X DELETE -H "Content-Type: application/json" \
  -H "Authorization: Bearer $ACCESSTOKEN" \
  https://embeddedassistant.googleapis.com/v1alpha2/projects/project_id/devices/id
  ```
- Send an HTTPS request with the following attributes:

  - URL: [https://embeddedassistant.googleapis.com/v1alpha2/projects/\<project_id\>/devices/\<id\>](https://embeddedassistant.googleapis.com/v1alpha2/projects/&lt;project_id&gt;/devices/&lt;id&gt;)
  - Method: `DELETE`
  - Body: None
  - Headers: `Authorization: Bearer <access-token>` where \<access-token\> is the string containing the [access token](https://developers.google.com/assistant/sdk/reference/device-registration/register-device-manual#get-access-token)