Events are asynchronous and managed by Google Cloud Pub/Sub, in a single topic per
Project. Events provide updates for all devices and structures and receipt of events is
assured as long as the access token is not revoked by the user and the event messages have not
expired.

> [!NOTE]
> **Note:** If your Device Access integration relies on Pub/Sub events and rarely makes API calls, the user's authorization will be revoked if the refresh token expires. **For continued access, you must use the refresh token to refresh the access token within 6
> months.** For further help with authorization, see [Refresh token expiration](https://developers.google.com/identity/protocols/oauth2#expiration).

Events are an optional feature of the SDM API. They can
easily be implemented and verified by using your Google Cloud
project.

## Enable events

Events may be enabled in the Device Access Console. Select your Project in the
Console, if you haven't already.

[Go to the Device Access Console](https://console.nest.google.com/device-access)

If you've already enabled events (for example, during [project
creation](https://developers.google.com/nest/device-access/get-started#create_a_device_access_project)),
the **Pub/Sub topic** field in the *Enable Pubsub topic for Events* section
should already have a value, called a Topic ID, in the older format of:

```
projects/sdm-prod/topics/enterprise-project-id
```

> [!WARNING]
> **Warning:** Event notifications will no longer be published to Google-hosted Pub/Sub topics after **November 30, 2026**. If your project uses a Google-hosted topic in the preceding format, you must migrate to a self-hosted Pub/Sub topic created within your own Google Cloud project before the deadline. After November 30, 2026, event streaming on Google-hosted topics will be discontinued, and your application will no longer receive real-time device state updates or trait events. If your project already uses a self-hosted topic in your own Google Cloud project, no action is needed.

To migrate from a Google-hosted topic, disable the Google-hosted Pub/Sub topic
in the Device Access Console and re-enable events using a self-hosted Pub/Sub
topic created in your own Google Cloud project. Migrating your
Pub/Sub topic configuration does not invalidate existing user access tokens or
OAuth permissions.

Self-hosted Pub/Sub topics can be created or changed in your own
Google Cloud project and are subject to standard
[Google Cloud Pub/Sub pricing](https://cloud.google.com/pubsub/pricing)
(which includes a free tier of 10 GB per month).

Create the Pub/Sub topic:

    gcloud pubsub topics create {topic} --message-retention-duration=0s

Grant the necessary SDM API permissions to access and
publish event data for your project's Topic ID:

    gcloud pubsub topics add-iam-policy-binding projects/{project}/topics/{topic} \
    --member="group:sdm-publisher@googlegroups.com" \
    --role="roles/pubsub.publisher"

The format of your newly created Topic ID will look like:

```
projects/gcp-project-name/subscriptions/topic-id
```

Take note of your Topic ID, you'll need it to complete
enabling events. See
[Create a topic](https://cloud.google.com/pubsub/docs/create-topic#pubsub_create_topic-gcloud)
for further information.

To enable events (or migrate from a Google-hosted topic):

1. If creating a new project, select **Enable events** and click **Create
   Project**. If migrating an existing project from a Google-hosted topic, first disable the existing Pub/Sub topic in the project's information screen.
2. Click the icon for **Pub/Sub
   topic** and select **Enable events with PubSub topic** (or **Edit current
   topic**).
3. Enter the self-hosted Pub/Sub Topic ID that you created earlier.

Copy your Topic ID, you'll need it to create a subscription to the topic so
event messages can be retrieved.

## Create a pull subscription

A pull subscription is where a subscriber initiates requests to the Pub/Sub
server for queued event messages. This is a quick and easy way to verify that
events are being generated for your authorized devices.

Open Cloud Shell for your Google Cloud project:

[Go to Google Cloud Shell](https://console.cloud.google.com/home/dashboard?cloudshell=true)

At the Cloud Shell prompt, run the following command to create a pull
subscription to your topic, using whatever <var translate="no">subscription-id</var> you like
and your unique Topic ID:

    gcloud pubsub subscriptions create subscription-id --topic=projects/gcp-project-name/subscriptions/topic-id
    Created subscription [projects/gcp-project-name/subscriptions/subscription-id].

## Initiate events

To initiate events for the first time once the Pub/Sub subscription has been
created, make a call to list your devices as a one-time trigger:

    curl -X GET 'https://smartdevicemanagement.googleapis.com/v1/enterprises/project-id/devices' \
        -H 'Content-Type: application/json' \
        -H 'Authorization: Bearer access-token'

Events for all structures and devices will publish after this API call.

## Generate events

Events are generated for all trait field changes, some of which are automatic,
some manual.

For example, if the ambient temperature near your
Nest Thermostat changes, an event for the
[Temperature](https://developers.google.com/nest/device-access/traits/device/temperature) trait will
automatically be sent with a new `ambientTemperatureCelsius` value.

To manually generate one, either:

1. Physically change the state of your device, such as changing the mode of your Nest Thermostat.
2. Trigger an event, such as motion, person, or sound on a Google Nest Cam Indoor.
3. [Execute a device command](https://developers.google.com/nest/device-access/use-the-api#execute_a_command) using the SDM API.

## View messages

Event messages can be viewed in your Google Cloud project, in the
Pub/Sub Subscriptions section:

[Go to Google Cloud Pub/Sub Subscriptions](https://console.cloud.google.com/cloudpubsub/subscription)

1. Click on the Subscription ID you created earlier.
2. On the *Subscription detail* screen, you should see activity for the **Unacked message count** chart. This indicates event messages have come through for your subscription. If you don't see any activity, wait a little while or generate a few more events.
3. Click **VIEW MESSAGES** to bring up the **Messages** panel.
4. Click **PULL** to view the messages. They should correspond to the actions you used to generate events.

Messages can also be viewed in Cloud Shell with a basic `pull` command:

    gcloud pubsub subscriptions pull subscription-id

For more information on subscription types, as well as the types of events sent
by the SDM API and how to use them, see
[Events](https://developers.google.com/nest/device-access/api/events).

## Manage messages

Messages should be regularly acknowledged and purged, to keep a subscription
clean and avoid duplicate message delivery to other subscribers. All
subscription messages can manually be purged in the Pub/Sub Subscriptions
section:

[Go to Google Cloud Pub/Sub Subscriptions](https://console.cloud.google.com/cloudpubsub/subscription)

1. Click on the Subscription ID.
2. There are a few ways to acknowledge and purge:
   1. Click **VIEW MESSAGES** to bring up the **Messages** panel. Check the **Enable ack messages** checkbox and click **PULL** to view and acknowledge all messages.
   2. Click **PURGE
      MESSAGES** to purge all existing messages by acknowledging without viewing them. Click **PURGE** to confirm.

Messages can also be purged in Cloud Shell by using the `--auto-ack` flag with
a basic `pull` command:

    gcloud pubsub subscriptions pull subscription-id --auto-ack

## Manage subscriptions

Subscriptions may be configured in a variety of ways, as described in
[Using subscription properties](https://cloud.google.com/pubsub/docs/admin#creating_subscriptions).

While how you want to manage subscriptions and messages is up to you, for a
production application we recommend that you use
[Service accounts](https://cloud.google.com/iam/docs/service-accounts) for
authentication, rather than a user account like you have so far in this quick
start guide. A service account is used by an application or virtual machine, not
a person, and has its own unique account key.

For more information on service account authentication with Device Access, see
[Events](https://developers.google.com/nest/device-access/api/events#service_accounts).