Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

particle-api-js

particle-iot12kApache-2.011.1.3

Particle API Client

particle, library, spark, api

readme

particle-api-js

JS Library for the Particle Cloud API for Node.js and the browser

Build Status

Installation | Development | Conventions | Docs | Examples | Building | Releasing | License

Installation

particle-api-js is available from npm to use in Node.js, bower or jsDelivr CDN for use in the browser.

Npm

$ npm install particle-api-js

Bower

$ bower install particle-api-js

jsDelivr CDN

<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/particle-api-js@8/dist/particle.min.js">
</script>

Development

All essential commands are available at the root via npm run <script name> - e.g. npm run lint. To view the available commands, run: npm run

<summary>How to run your tests</summary>

The Agent integration tests (source) depend on a real HTTP api backend and a valid Particle access token. Be sure to set relevant environment variables to avoid test failures. You can prefix commands test commands like this PARTICLE_API_BASE_URL=<url> PARTICLE_API_TOKEN=<token> npm test

npm test runs the tests.

npm run coverage shows code coverage

npm run test:browser runs tests in a browser via karma.

npm run test:ci runs tests in the exact same way CI system does

<summary>How to write scripts that execute against local code changes?</summary>

Source code lives in the ./src directory and is built for release via the npm run build command. To create a simple script file to test your changes, follow these steps:

  1. create a js file on your local machine: touch my-api-test.js (somewhere outside of the root of this repo)
  2. within your test js file, init the api client like so:
const ParticleAPI = require('./path/to/particle-api-js'); // Make sure to substitute to correct path
const api = new ParticleAPI();
  1. add in any api calls, etc required to validate you changes
const devices = await api.listDevices({ auth: '<particle-auth-token>' });
console.log('MY DEVICES:', devices);
  1. run it: node ./path/to/my-api-test.js

NOTE: Requiring the root directory works via the main field specified in Particle API JS' package.json file (docs)

<summary>How to name npm scripts</summary>

npm scripts are the primary means of executing programmatic tasks (e.g. tests, linting, releasing, etc) within the repo. to view available scripts, run npm run.

when creating a new script, be sure it does not already exist and use the following naming convention:

<category>:[<subcategory>]:[<action>]

our standard categories include: test, lint, build, clean, docs, package, dependencies, and release. top-level scripts - e.g. npm run clean - will typically run all of its subcategories (e.g. npm run clean:dist && npm run clean:tmp).

npm itself includes special handling for test and start (doc: 1, 2) amongst other lifecycle scripts - use these to expose key testing and start-up commands.

sometimes your new script will be very similar to an existing script. in those cases, try to extend the existing script before adding another one.

Conventions

  • npm scripts form the developer's API for the repo and all of its packages - key orchestration commands should be exposed here
  • document developer-facing process / tooling instructions in the Development section
  • plan to release your changes upon merging to main - refrain from merging if you cannot so you don't leave unpublished changes to others
  • avoid making changes in files unrelated to the work you are doing so you aren't having to publish trivial updates
  • test files live alongside their source files and are named like *.test.js or *.spec.js
  • if the linter does not flag your code (error or warning), it's formatted properly
  • avoid reformatting unflagged code as it can obscure more meaningful changes and increase the chance of merge conflicts
  • todo comments include your last name and are formatted like: TODO (mirande): <message>

Docs & Resources

First, read the documentation for Particle API JS on the Documentation website. It contains examples to get started.

For more details, read the API reference on GitHub.

Examples

There are many snippets of using Particle API JS on the Documentation website and some complete examples in the GitHub examples directory.

Building

Make your changes to the files in the src directory, then from the project directory run:

$ npm run build

The dist directory will contain the compiled and minified files that can be included in your project.

Run tests to make sure your changes are good:

$ npm test

Update the API docs with your changes:

$ npm run docs

Releasing

See the release process in the RELEASE.md file.

License

Copyright © 2016 Particle Industries, Inc. Released under the Apache 2.0 license. See LICENSE for details.

changelog

changelog

11.1.3 - 22 July 2025

  • Bump form-data to resolve security warning

11.1.2 - 11 December 2024

  • Add context docs for x-particle-tool and x-particle-project headers

11.1.1 - 5 November 2024

  • Workaround for Firefox failing to open multiple event streams

11.1.0 - 3 October 2024

  • Re-add deleteAccessToken method, but with basic auth removed

11.0.0 - 1 October 2024

  • Remove listAccessTokens and deleteAccessToken methods that relied on HTTP basic auth
  • Update EventStream to send access_token in the Authorization header instead. No functional change

10.6.0 - 21 August 2024

  • Add DeviceOS versions endpoints

10.5.1 - 21 June 2024

  • Don't add an empty query string to the URL

10.5.0 - 14 June 2024

  • Add unprotectDevice
  • Remove changeProduct

10.4.3 - 25 March 2024

  • Add utm paramater to createUser

10.4.2 - 3 January 2024

  • Add setMode arg to setLedgerInstance
  • Fix a few incorrect logic function JSDocs

10.4.1 - 2 January 2024

  • Fix setLedgerInstance taking wrong argument and passing a bad request body to the API

10.4.0 - 19 December 2023

  • Add listLedgerInstanceVersions function
  • Add getLedgerInstanceVersion function

10.3.1 - 6 December 2023

  • Add todayStats query option to listLogicFunctions
  • Add scope, archived, page, and perPage query options to listLedgers
  • Add page and perPage query options to listLedgerInstances
  • Add JSDocs for constructor
  • Fix JSDocs across most functions in the library to be more accurate

10.3.0 - 7 November 2023

  • Add support for sandbox accounts on all logic/ledger APIs by allowing the omission of the org property
  • Add executeLogic function for testing logic functions before deploying them

10.2.0 - 6 October 2023

  • Migrate LogicBlocks methods to LogicFunctions, LogicTriggers, and LogicRuns

10.1.0 - 8 Sept 2023

  • Use wepback to bundle for browser use
  • Remove babel, browserify and their related dependencies
  • Adds support for checkJS loose type/docs validations

10.0.0 - 8 Sept 2023

  • Change library to handle requests from superagent to fetch/node-fetch

9.4.1 - 17 June 2022

  • Fixes incompatible versions of eslint, chai, sinon-chai and chai-as-promised.

9.4.0 - 14 June 2022

  • Adds .setDefaultAuth(auth) so token authenticated methods don't need to pass their own auth token.
  • Adds support for auth option in Particle constructor, calls .setDefaultAuth() if provided
  • Fixes bug where .setBaseUrl(baseUrl) was not honored for .getEventStream() method (sc-105035)

9.3.0 - 8 June 2022

  • Adds .setBaseUrl(baseUrl) to override backend api endpoint

9.2.0 - 30 May 2022

  • Move to node@16 and npm@8 for local development

9.1.2 - 9 December 2021

  • Fix library download

9.1.1 - 7 December 2021

  • Use unforked copy of stream-http dependency

9.1.0 - 8 December 2020

  • .listAccessTokens() accepts otp option to support users with MFA enabled

9.0.2 - 28 July 2020

  • Add .deleteActiveAccessTokens() method
  • Add invalidateTokens arguments to .confirmMfa() and .changeUsername() methods

9.0.1 - 1 June 2020

  • Add .getProductDeviceConfiguration() and .getProductDeviceConfigurationSchema() methods

9.0.0 - 20 May 2020

  • Add support for configuration and location services
  • All top-level api methods optionally accept a headers option object
  • Breaking: Base http methods (.get(), .put(), etc) on agent and particle classes accept options object vs. positional arguments (see here)
  • Breaking: .downloadFile() method uses uri option (vs. url) (docs)
  • Breaking: Refactored options object for the .createWebhook() method - hook-related options are now passed in via options.hook (docs)

8.4.0 - 28 April 2020

  • Allow invalidating access tokens when changing password

8.3.0 - 11 February 2020

  • Add delete user

8.2.1 - 4 February 2020

  • fix file download methods .downloadFile(), .downloadFirmwareBinary(), and .downloadProductFirmware() PR #112

8.2.0 - 28 January 2020

  • .addDeviceToProduct() accepts file option to facillitate bulk importing of devices PR #109

8.1.0 - 24 January 2020

  • Add support for groups query parameter when listing product devices via .listDevices() PR #108
  • Update eslint and related configuration PR #107

8.0.1 - 2 December 2019

  • Update to latest superagent to fix deprecation warnings in Node v12

8.0.0 - 30 July 2019

  • EventStream returned by getEventStream handles errors better PR #99. Breaking changes for EventStream:
    • Only emits a single event named 'event' for each Particle event received instead of 2 events, one named 'event' and another named after the Particle event name. This behavior caused EventStream to disconnects if a Particle event named 'error' was published.
    • Does not emit the 'error' event when a network error happens. Instead it emits 'disconnect' and automatically reconnects.

7.4.1 - 6 May 2019

  • Do not require network ID to remove a device from its network PR #103

7.4.0 - 27 Feb 2019

  • Add support for mesh network management PR #98

7.3.0 - 10 Jan 2019

  • Support flashing product devices PR #97

7.2.3 - 4 Aug 2018

  • Add sendOtp method to allow users enrolled in MFA/Two-Step Auth to login PR #92

7.2.2 - 23 Jul 2018

  • Fix npm api key for publishing to registry

7.2.1 - 23 Jul 2018

  • Support enrolling user in MFA/Two-step authentication

7.2.0 - 22 Mar 2018

  • Support changing user's username(i.e., email) and password PR #84

7.1.1 - 13 Feb 2018

  • Fix country parameter for activate sim PR #81

7.1.0 - 17 Jan 2018

  • Update jsDelivr link PR #66. Thanks @LukasDrgon!
  • Stop auto reconnecting when event stream is intentionally disconnected PR #69. Thanks @spacetc62!
  • Add createCustomer PR #78. Thanks @monkeytronics!
  • Fix event stream exception when it is an HTML response PR #64. Thanks @spacetc62!
  • Update links after GitHub organization rename to particle-iot PR #79

7.0.1 - 16 Nov 2017

  • Add loginAsClientOwner method

7.0.0 - 7 Nov 2017

  • Update to latest superagent with support for nested directory. Drops support for Node versions earlier than 4.
  • Add serial number endpoint

6.6.2 - 15 Sep 2017

  • Fix nested directories bug

6.6.1 - 14 Sep 2017

  • Update form-data to v1.0.0-relativepath.2

6.6.0 - 12 Sep 2017

  • Add support for deleting current token

6.5.0 - 02 May 2017

  • Add support for all product API endpoints.
  • Add support for sending additional context with each call.

6.4.3 - 15 Feb 2017

  • Create a wrapper for listBuildTargets in Client.js.
  • Marked compileCode, signalDevice, listDevices and listBuildTargets as deprecated. Those methods will be removed in 6.5

6.4.2 - 05 Jan 2017

  • Create a wrapper for listDevices in Client.js.

6.4.1 - 15 Dec 2016

  • Add scopes to library listing

6.4.0 - 09 Nov 2016

  • Create a wrapper for signalDevice in Client.js.

6.3.0 - 31 Oct 2016

  • Add support for account verification endpoint via verifyUser function
  • Change account_info input parameter in createUser and setUserInfo to be camel case - accountInfo

6.2.0 - 19 Oct 2016

  • Add support for account information fields in createUser and setUserInfo
  • Add "shortErrorDescription" in response body to contain English description only

6.1.0 - 19 Oct 2016

  • Add library publish

6.0.8 - 17 Oct 2016

  • Rename library publish to library contribute

6.0.7 - 29 Sept 2016

  • Add library versions endpoint

6.0.6 - 19 Sept 2016

  • Add library delete

6.0.5 - 8 Sept 2016

  • Add library publish

6.0.4 - 30 Aug 2016

  • Use only HTTP dependencies to be able to install on computers without git

6.0.3 - 25 Aug 2016

  • Support nested directories when compiling sources

6.0.2 - 23 Aug 2016

  • Add compile code to client

6.0.1 - 22 Aug 2016

  • Fix the login method content type

6.0.0 - 17 Aug 2016

  • Add libraries endpoints
  • Add stateful client
  • Add object interface for libraries

5.3.1 - 2 Aug 2016

  • Handle empty event names in the event stream.

5.3.0 - 8 June 2016

  • Add details to README
  • Adding responseTemplate and responseTopic to webhook creation. Thanks @acasas! #20
  • Add password reset route #27
  • Make event stream compatible with new product routes #28

5.2.7 - 2 May 2016

  • Fix files parameter default name to be file and not file1.

5.2.6 - 25 Mar 2016

  • Don't double publish event stream events if the event is named event.

5.2.5 - 21 Mar 2016

  • Handle JSON.parse exceptions when parsing event stream

5.2.4 - 21 Mar 2016

  • flashDevice latest also needs to be a string, not a boolean. #12

5.2.3 - 11 Mar 2016

  • Remove setting of User-Agent header because that is not allowed in browsers. #10

5.2.2 - 3 Mar 2016

  • Fix named event streams by encoding event name.
  • Move access token to query string to eliminate preflight CORS request.
  • Use fork of stream-http that prevents usage of fetch because it does not abort.
  • Use correct streaming mode of stream-http.

5.2.1 - 3 Mar 2016

  • Improve cleanup on abort.

5.2.0 - 3 Mar 2016

  • Add support for organization and product slugs to getEventStream.

5.1.1 - 26 Feb 2016

  • JSON.parse HTTP response body for getEventStream error case.

5.1.0 - 26 Feb 2016

  • Fix event stream. #8
  • Add downloadFirmwareBinary
  • Add ability to intercept requests for debugging
  • Use library version for User-Agent
  • Allow request transfer for claimDevice
  • signalDevice needs to use strings, not numbers.
  • compileCode latest should be a string, not a boolean.

5.0.2 - 24 Feb 2016

  • Remove trailing slash from baseUrl. #7

5.0.1 - 18 Feb 2016

  • Remove need for require('particle-api-js').default in CommonJS usage. It is now just require('particle-api-js').

5.0.0 - 18 Feb 2016

  • Removed need for babel-runtime.
  • Add flashDevice, compileCode, and listAccessTokens.
  • Add missing options to createWebhook.
  • Remove downloadFirmwareBinary.

4.2.1 - 8 Feb 2016

  • Update contributors.

4.2.0 - 8 Feb 2016

  • Add downloadFirmwareBinary.

4.1.0 - 14 Jan 2016

  • Add validatePromoCode.
  • activateSIM now requires promo_code and action.

4.0.2 - 16 Nov 2015

  • Fix old code reference.

4.0.1 - 16 Nov 2015

  • Change code to statusCode in rejection.

4.0.0 - 16 Nov 2015

  • Add statusCode to Promise fulfillment.

3.0.3 - 6 Nov 2015

  • Add listBuildTargets.

3.0.2 - 5 Nov 2015

  • Add countryCode to activateSIM.

3.0.1 - 26 Oct 2015

  • Fix activateSIM.

3.0.0 - 26 Oct 2015

  • Replace request with superagent.
  • Add iccid to getClaimCode.
  • Only use form encoding on login and signup.

2.0.1 - 23 Oct 2015

  • Removed browser entry in package.json. This makes it possible to bundle the module with other apps that use browserify without causing relative pathing issues.

2.0.0 - 20 Oct 2015

  • Improved error handling and reporting. Network errors and HTTP errors now both return code property that can be more easily used to programmatically detect error types.

1.0.1 - 24 Sep 2015

1.0.0 - 24 Sep 2015