Google Cloud IoT Core is being retired on August 16, 2023. Contact your Google Cloud account team for more information.

Create a device using ES256

Stay organized with collections Save and categorize content based on your preferences.

Create a device using ES256 for authentication.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample


For more information, see the Cloud IoT Core Go API reference documentation.

// createES creates a device in a registry with ES256 credentials.
func createES(w io.Writer, projectID string, region string, registryID string, deviceID string, keyPath string) (*cloudiot.Device, error) {
	// Authorize the client using Application Default Credentials.
	// See
	ctx := context.Background()
	httpClient, err := google.DefaultClient(ctx, cloudiot.CloudPlatformScope)
	if err != nil {
		return nil, err
	client, err := cloudiot.New(httpClient)
	if err != nil {
		return nil, err

	keyBytes, err := ioutil.ReadFile(keyPath)
	if err != nil {
		return nil, err

	device := cloudiot.Device{
		Id: deviceID,
		Credentials: []*cloudiot.DeviceCredential{
				PublicKey: &cloudiot.PublicKeyCredential{
					Format: "ES256_PEM",
					Key:    string(keyBytes),

	parent := fmt.Sprintf("projects/%s/locations/%s/registries/%s", projectID, region, registryID)
	response, err := client.Projects.Locations.Registries.Devices.Create(parent, &device).Do()
	if err != nil {
		return nil, err

	fmt.Fprintf(w, "Successfully created ES256 device: %s\n", deviceID)

	return response, nil


For more information, see the Cloud IoT Core Java API reference documentation.

/** Create a device that is authenticated using ES256. */
protected static void createDeviceWithEs256(
    String deviceId,
    String publicKeyFilePath,
    String projectId,
    String cloudRegion,
    String registryName)
    throws GeneralSecurityException, IOException {
  GoogleCredentials credential =
  JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
  HttpRequestInitializer init = new HttpCredentialsAdapter(credential);
  final CloudIot service =
      new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init)

  final String registryPath =
          "projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName);

  PublicKeyCredential publicKeyCredential = new PublicKeyCredential();
  final String key = Files.toString(new File(publicKeyFilePath), Charsets.UTF_8);

  DeviceCredential devCredential = new DeviceCredential();

  System.out.println("Creating device with id: " + deviceId);
  Device device = new Device();

  Device createdDevice =
          .create(registryPath, device)

  System.out.println("Created device: " + createdDevice.toPrettyString());


For more information, see the Cloud IoT Core Node.js API reference documentation.

// const cloudRegion = 'us-central1';
// const deviceId = 'my-es-device';
// const projectId = 'adjective-noun-123';
// const registryId = 'my-registry';
const iot = require('@google-cloud/iot');

const iotClient = new iot.v1.DeviceManagerClient({
  // optional auth parameters.

async function createDevice() {
  // Construct request
  const regPath = iotClient.registryPath(projectId, cloudRegion, registryId);
  const device = {
    id: deviceId,
    credentials: [
        publicKey: {
          format: 'ES256_PEM',
          key: readFileSync(esCertificateFile).toString(),
  const request = {
    parent: regPath,

  const [response] = await iotClient.createDevice(request);
  console.log('Created device', response);



For more information, see the Cloud IoT Core PHP API reference documentation.

use Google\Cloud\Iot\V1\DeviceManagerClient;
use Google\Cloud\Iot\V1\Device;
use Google\Cloud\Iot\V1\DeviceCredential;
use Google\Cloud\Iot\V1\PublicKeyCredential;
use Google\Cloud\Iot\V1\PublicKeyFormat;

 * Create a new device with the given id, using ES256 for
 * authentication.
 * @param string $registryId IOT Device Registry ID
 * @param string $deviceId IOT Device ID
 * @param string $publicKeyFile Path to public ES256 key file.
 * @param string $projectId (optional) Google Cloud project ID
 * @param string $location (Optional) Google Cloud region
function create_es_device(
    $location = 'us-central1'
) {
    print('Creating new ES256 Device' . PHP_EOL);

    // Instantiate a client.
    $deviceManager = new DeviceManagerClient();
    $registryName = $deviceManager->registryName($projectId, $location, $registryId);

    $publicKey = (new PublicKeyCredential())

    $credential = (new DeviceCredential())

    $device = (new Device())

    $device = $deviceManager->createDevice($registryName, $device);

    printf('Device: %s : %s' . PHP_EOL,


For more information, see the Cloud IoT Core Python API reference documentation.

# project_id = 'YOUR_PROJECT_ID'
# cloud_region = 'us-central1'
# registry_id = 'your-registry-id'
# device_id = 'your-device-id'
# public_key_file = 'path/to/certificate.pem'

client = iot_v1.DeviceManagerClient()

parent = client.registry_path(project_id, cloud_region, registry_id)

with as f:
    public_key =

# Note: You can have multiple credentials associated with a device.
device_template = {
    "id": device_id,
    "credentials": [
            "public_key": {
                "format": iot_v1.PublicKeyFormat.ES256_PEM,
                "key": public_key,

return client.create_device(request={"parent": parent, "device": device_template})


For more information, see the Cloud IoT Core Ruby API reference documentation.

# project_id  = "Your Google Cloud project ID"
# location_id = "The Cloud region the registry is located in"
# registry_id = "The registry to create a device in"
# device_id   = "The identifier of the device to create"
# cert_path   = "The path to the EC certificate"

require "google/apis/cloudiot_v1"

# Initialize the client and authenticate with the specified scope
Cloudiot   = Google::Apis::CloudiotV1
iot_client =
iot_client.authorization = Google::Auth.get_application_default(

# The resource name of the location associated with the project
parent = "projects/#{project_id}/locations/#{location_id}/registries/#{registry_id}"

device = = device_id

pubkey =
pubkey.key = cert_path
pubkey.format = "ES256_PEM"

cred =
cred.public_key = pubkey

device.credentials = [cred]

# Create the device
device = iot_client.create_project_location_registry_device parent, device

puts "Device: #{}"
puts "\tBlocked: #{device.blocked}"
puts "\tLast Event Time: #{device.last_event_time}"
puts "\tLast State Time: #{device.last_state_time}"
puts "\tName: #{}"

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.