# Device registration and sending to a User

> \[!IMPORTANT]
>
> The Push Notifications API is currently available as a
> Private Beta product. The information contained in this document is subject to change. You acknowledge
> and agree that your use of the Twilio Push Notifications API is subject to the terms of the [Services in
> Private Beta](https://www.twilio.com/en-us/legal/service-country-specific-terms/private-beta). This
> means that some features are not yet implemented and others may be changed before the product is
> declared as Generally Available. Private Beta products are not covered by the Twilio Support Terms or
> Twilio Service Level Agreement.

In many production use cases, it is advantageous to store the device token information with Twilio. This allows you to:

* Push to all of a User's devices.
* Reference Users instead of individually referencing their tokens.
* Send pushes to repeat groups of Users through Audiences.

A [DeviceRegistration](/docs/push-notifications/api/device-registration-resource) stores a device token so that you can push to it later. It maps to a specific [User](/docs/push-notifications/api/user-resource) and [App](/docs/push-notifications/api/app-resource). This structure allows you to push to the same User for different applications, and to push to all of a User's devices for a specific application at once.

## Register a device

You can register a device by making a `POST` request to the `/PushNotifications/DeviceRegistrations` endpoint.

The following example shows how to register a device by providing an existing `userId`:

```bash {title="Register a device with a userId"}
curl -X POST 'https://comms.twilio.com/v1/PushNotifications/DeviceRegistrations' \
     -H 'Content-Type: application/json' \
     -d '{
          "userId": "comms_user_01h9krwprkeee8fzqspvwy6nq8",
          "token": "odJFCrnl2edlBDdz1C5Ja:APA91bu2RJtBRnlWmTSHf6pWkLUyifDLkDmWJ6UuVTAIjvFu7WICPhDeOZIiBOB-Y6sHrFH2ZUCr-lgotu2iXW7GboIRoL3u6aHwnMztVuaP_coUNEhEkk_iqq8vH2BzNZV45pFCiRcD",
          "appName": "test_app",
          "provider": "FCM"
        }' \
     -u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN
```

The `userId` is optional. If you omit it, the API automatically creates a User to store the DeviceRegistration.

```bash {title="Register a device without a userId"}
curl -X POST 'https://comms.twilio.com/v1/PushNotifications/DeviceRegistrations' \
     -H 'Content-Type: application/json' \
     -d '{
          "token": "odJFCrnl2edlBDdz1C5Ja:APA91bu2RJtBRnlWmTSHf6pWkLUyifDLkDmWJ6UuVTAIjvFu7WICPhDeOZIiBOB-Y6sHrFH2ZUCr-lgotu2iXW7GboIRoL3u6aHwnMztVuaP_coUNEhEkk_iqq8vH2BzNZV45pFCiRcD",
          "appName": "test_app",
          "provider": "FCM"
        }' \
     -u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN
```

## Fetch a DeviceRegistration

You can retrieve the details of a DeviceRegistration, including the associated User, by making a `GET` request:

```bash {title="Fetch a DeviceRegistration"}
curl -X GET 'https://comms.twilio.com/v1/PushNotifications/DeviceRegistrations/{DeviceRegistrationId}' \
     -H 'Content-Type: application/json' \
     -u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN
```

**Sample response**:

```json
{
   "appName": "test_app",
   "userId": "comms_user_01k7q2bx0mft5bvbxh3781hkmr",
   "devices": [
      {
         "provider": "FCM",
         "token": "odJFCrnl2edlBDdz1C5Ja:APA91bu2RJtBRnlWmTSHf6pWkLUyifDLkDmWJ6UuVTAIjvFu7WICPhDeOZIiBOB-Y6sHrFH2ZUCr-lgotu2iXW7GboIRoL3u6aHwnMztVuaP_coUNEhEkk_iqq8vH2BzNZV45pFCiRcD"
      }
   ],
   "id": "comms_device_registration_175903a30e55ce6dd9ac28d2704867dd",
   "related": [
      {
         "id": "comms_user_01k7q2bx0mft5bvbxh3781hkmr",
         "name": "user",
         "uri": "/Users/comms_user_01k7q2bx0mft5bvbxh3781hkmr"
      },
      {
         "id": "test_app",
         "name": "push_notification_app",
         "uri": "/PushNotifications/Apps/test_app"
      }
   ],
   "tags": {}
}
```

## Send a notification to a User

Once you have registered devices to a User, you can send a notification to that User. The Push Notifications API delivers the push to all registered devices for the default App.

```bash {title="Send a notification to a User"}
curl -X POST 'https://comms.twilio.com/v1/PushNotifications' \
     -H 'Content-Type: application/json' \
     -d '{
          "to": [
            {
              "userId": "comms_user_01h9krwprkeee8fzqspvwy6nq8"
            }
          ],
          "content": {
            "title": "Welcome Back!",
            "body": "It looks like you have been away for a while. New offers are waiting for you."
          }
        }' \
     -u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN
```

This request delivers the notification to all device tokens associated with the specified User under the default App's Credentials. If the default App does not have a Credential for a specific provider (such as FCM or APNs) that the User has registered, the notification is not delivered to those tokens.

## Next steps

* Learn how to [personalize push notifications](/docs/push-notifications/personalization) for each recipient.
* Review the [DeviceRegistrations API reference](/docs/push-notifications/api/device-registration-resource).
* Review the [Users API reference](/docs/push-notifications/api/user-resource).
