# Creating devices

This page explains how to create devices.

## Before you begin

> [!NOTE]
> **Note:** Before you use any of the Cloud Identity APIs, you must set up Cloud Identity. See [Setting up Cloud Identity](https://docs.cloud.google.com/identity/docs/set-up-cloud-identity-admin) for instructions.

Perform the following tasks before proceeding with the information on this page:

- Read the [Devices API overview](https://docs.cloud.google.com/identity/docs/concepts/overview-devices.md.txt).

- [Set up the Devices API](https://docs.cloud.google.com/identity/docs/how-to/setup.md.txt).

## Creating a device

The following examples shows you how to create a device.

### REST

To create a device, call
[`devices.create()`](https://docs.cloud.google.com/identity/docs/reference/rest/v1beta1/devices/create.md.txt)
with the name of the `customer` (organization) and a
[Device object](https://docs.cloud.google.com/identity/docs/reference/rest/v1/devices.md.txt).

### Python HTTP

The following example shows a helper function to create a device
using the Python HTTP library:

    """Example script to use the create method of the Devices API."""
    import json
    import pprint

    from six.moves import urllib

    import google.auth.transport.requests
    from google.oauth2 import service_account

    SCOPES = ['https://www.googleapis.com/auth/cloud-identity.devices']
    BASE_URL = 'https://cloudidentity.googleapis.com/v1/'

    # Change this to the location of the service account key
    SA_FILE = ''

    # Enter the administrator to call as here.
    ADMIN_EMAIL = ''

    if not SA_FILE:
      print('Please specify the location of the service account key file')
    if not ADMIN_EMAIL:
      print('Please specify the email of the administrator to call as')

    if not SA_FILE or not ADMIN_EMAIL:
      exit(-1)

    def create_delegated_credentials(user_email):
      credentials = service_account.Credentials.from_service_account_file(
          SA_FILE,
          scopes=['https://www.googleapis.com/auth/cloud-identity.devices'])

      delegated_credentials = credentials.with_subject(user_email)

      return delegated_credentials

    ######################################################################
    # AUTHENTICATE the service account and retrieve an oauth2 access token

    request = google.auth.transport.requests.Request()
    dc = create_delegated_credentials(ADMIN_EMAIL)
    dc.refresh(request)
    print('Access token: ' + dc.token + '\n')

    ###############################
    # Create the Device
    header = {
        'authorization': 'Bearer ' + dc.token,
        'Content-Type': 'application/json'
    }
    body = {
        'serialNumber': 'someSerial',  # the serial number of your device.

        # see values in
        # https://cloud.google.com/identity/docs/reference/rest/v1/devices#DeviceType
        'deviceType': 'LINUX'
    }

    serialized_body = json.dumps(body, separators=(',', ':'))

    request_url = BASE_URL + 'devices'
    print('Request URL: ' + request_url)
    print('Request body: ' + serialized_body)

    serialized_body = json.dumps(body, separators=(',', ':'))
    request = urllib.request.Request(request_url, serialized_body, headers=header)
    request.get_method = lambda: 'POST'

    try:
      contents = urllib.request.urlopen(request)
    except urllib.error.HTTPError as e:
      if e.code == 409:
        print('The request was invalid. Perhaps the device is already present?')
      else:
        print('Unknown error occurred: {}', e.code)
      exit(-1)

    create_response = json.loads(contents.read())
    inner_response = create_response['response']
    pp = pprint.PrettyPrinter(indent=4)
    pp.pprint(inner_response)