Exploring Particle.io Device Cloud API with JavaScript

Particle.io is a powerful Internet of things cloud platform that enables developers to design, develop and deploy enterprise-level IoT applications. The Particle Cloud offers a wide range of application programming interfaces (APIs) that seamlessly integrate with your IoT devices. In this blog, we'll explore some of Particle.io's APIs and demonstrate how to use them with JavaScript.

Particle.io API Introduction

Particle.io exposes APIs that enable developers to control and monitor their fleet of IoT devices securely. The APIs are organized into several categories, including device management, event streaming, functions, and variables. This tutorial focuses on the device management category, which includes the following endpoints:

Claim a Device

The claim device endpoint enables you to claim a new device to your account. When you call this endpoint, the device is removed from the list of unclaimed devices and added to the list of claimed devices.

Example code:

const fetch = require('node-fetch');

const accessToken = 'YOUR_ACCESS_TOKEN_HERE';
const deviceId = 'YOUR_DEVICE_ID_HERE';

const requestOptions = {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${accessToken}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    id: deviceId,
  }),
};

fetch('https://api.particle.io/v1/devices', requestOptions)
  .then((response) => response.json())
  .then((data) => {
    console.log(data);
  })
  .catch((error) => {
    console.log(error);
  });

Unclaim a Device

The unclaim device endpoint enables you to unclaim a device from your account. When you call this endpoint, the device is removed from the list of claimed devices and added to the list of unclaimed devices.

Example code:

const fetch = require('node-fetch');

const accessToken = 'YOUR_ACCESS_TOKEN_HERE';
const deviceId = 'YOUR_DEVICE_ID_HERE';

const requestOptions = {
  method: 'DELETE',
  headers: {
    Authorization: `Bearer ${accessToken}`,
    'Content-Type': 'application/json',
  },
};

fetch(`https://api.particle.io/v1/devices/${deviceId}`, requestOptions)
  .then((response) => response.json())
  .then((data) => {
    console.log(data);
  })
  .catch((error) => {
    console.log(error);
  });

Rename a Device

The rename device endpoint enables you to rename a device. When you call this endpoint, the device's name is updated to the new name.

Example code:

const fetch = require('node-fetch');

const accessToken = 'YOUR_ACCESS_TOKEN_HERE';
const deviceId = 'YOUR_DEVICE_ID_HERE';
const newName = 'NEW_DEVICE_NAME_HERE';

const requestOptions = {
  method: 'PUT',
  headers: {
    Authorization: `Bearer ${accessToken}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    name: newName,
  }),
};

fetch(`https://api.particle.io/v1/devices/${deviceId}`, requestOptions)
  .then((response) => response.json())
  .then((data) => {
    console.log(data);
  })
  .catch((error) => {
    console.log(error);
  });

Conclusion

Particle.io device cloud API provides an easy way to manage and monitor your fleet of IoT devices. With this tutorial, you learned how to use some of Particle.io's device management API endpoints with JavaScript. If you want to learn more about other categories of Particle.io's API, head to Particle.io API docs.

Related APIs

Public APIs — A directory of free and public apis

Built by @mddanishyusuf