### Get Instructions for Device Type Installation (Node.js)
Source: https://docs.balena.io/reference/sdk/node-sdk/latest/index
Retrieves step-by-step instructions for installing a host OS onto a given device type. This method accepts either the device type slug or a contract object. It returns a Promise that resolves with an array of instruction strings.
```javascript
balena.models.deviceType.getInstructions('raspberry-pi').then(function(instructions) {
for (let instruction of instructions.values()) {
console.log(instruction);
}
// Insert the sdcard to the host machine.
// Write the BalenaOS file you downloaded to the sdcard. We recommend using Etcher.
// Wait for writing of BalenaOS to complete.
// Remove the sdcard from the host machine.
// Insert the freshly flashed sdcard into the Raspberry Pi (v1 / Zero / Zero W).
// Connect power to the Raspberry Pi (v1 / Zero / Zero W) to boot the device.
});
```
--------------------------------
### Get Installation Method for Device Type (Node.js)
Source: https://docs.balena.io/reference/sdk/node-sdk/latest/index
Fetches the supported installation method for a specific device type identified by its slug. This can indicate how BalenaOS is typically installed on that device. It returns a Promise that resolves with the installation method as a string.
```javascript
balena.models.deviceType.getInstallMethod('raspberry-pi').then(function(method) {
console.log(method);
// externalBoot
});
```
--------------------------------
### Install Balena SDK with npm
Source: https://docs.balena.io/reference/sdk/node-sdk/latest/index
Installs the balena-sdk package as a dependency for your Node.js project using npm.
```bash
$ npm install --save balena-sdk
```
--------------------------------
### Create Billing Setup Intent
Source: https://docs.balena.io/reference/sdk/node-sdk/latest/index
Creates a Stripe setup intent, which is necessary for setting up billing information. It requires an object containing setup intent parameters, including the organization's handle or ID and optionally a reCAPTCHA response. This method is exclusive to Balena.io. Returns a promise that resolves with a partial Stripe setup intent object.
```javascript
balena.models.billing.createSetupIntent(orgId).then(function(setupIntent) {
console.log(setupIntent);
});
```
--------------------------------
### Get Balena SDK Instance with Options
Source: https://docs.balena.io/reference/sdk/node-sdk/latest/index
Demonstrates how to get an instance of the Balena SDK, optionally providing configuration options like apiUrl and dataDirectory. Supports both ES6 imports and Node.js require.
```javascript
// with es6 imports
import { getSdk } from 'balena-sdk';
// or with node require
const { getSdk } = require('balena-sdk');
const balena = getSdk({
apiUrl: "https://api.balena-cloud.com/",
dataDirectory: "/opt/local/balena"
});
```
--------------------------------
### Start OS Update on Device (JavaScript)
Source: https://docs.balena.io/reference/sdk/node-sdk/latest/index
Initiates an operating system update on one or more devices. It requires the device UUID(s) and the target OS version, which must be semver-compatible, unpublished, and greater than the currently installed version. An optional options object can specify if the update should run in detached mode (defaulting to true). The method returns a Promise that resolves with the action status.
```javascript
balena.models.device.startOsUpdate('7cf02a687b74206f92cb455969cf8e98', '2.29.2+rev1.prod').then(function(status) {
console.log(result.status);
});
```
--------------------------------
### Get Two-Factor Setup Key - Node.js
Source: https://docs.balena.io/reference/sdk/node-sdk/latest/index
Retrieves the setup key required for configuring a two-factor authentication authenticator app. This key is usually presented as a QR code or a text string.
```javascript
balena.auth.twoFactor.getSetupKey()
```
--------------------------------
### Get Two-Factor Authentication Setup Key (Node.js)
Source: https://docs.balena.io/reference/sdk/node-sdk/latest/index
Retrieves the setup key required for enabling two-factor authentication (2FA) for an account. This method returns a Promise that resolves to a string representing the setup key. This key is typically used with authenticator apps.
```javascript
const setupKey = balena.auth.twoFactor.getSetupKey();
console.log(setupKey);
```
--------------------------------
### Device Type API
Source: https://docs.balena.io/reference/sdk/node-sdk/latest/index
This section details the API endpoints for retrieving and managing device type information. These methods allow you to fetch all supported device types, get specific device types by their slug or name, and retrieve associated metadata like display names, slugs, contracts, installation instructions, and installation methods.
```APIDOC
## GET /deviceType.getAllSupported
### Description
Retrieves a list of all device types supported by the Balena platform.
### Method
GET
### Endpoint
/deviceType.getAllSupported
### Parameters
#### Query Parameters
- **options** (Object) - Optional - Extra Pine options to use for the query.
### Request Example
```javascript
balena.models.deviceType.getAllSupported().then(function(deviceTypes) {
console.log(deviceTypes);
});
```
### Response
#### Success Response (200)
- **deviceTypes** (Object[]) - An array of supported device type objects.
#### Response Example
```json
[
{
"id": 1,
"slug": "raspberry-pi",
"name": "Raspberry Pi",
"is_virtual": false,
"data": {
"is_hidden": false
}
}
]
```
## GET /deviceType.getBySlugOrName
### Description
Retrieves a specific device type identified by its slug or name.
### Method
GET
### Endpoint
/deviceType.getBySlugOrName
### Parameters
#### Query Parameters
- **slugOrName** (String) - Required - The slug or name of the device type to retrieve.
### Request Example
```javascript
balena.models.deviceType.getBySlugOrName('raspberry-pi').then(function(deviceType) {
console.log(deviceType);
});
```
### Response
#### Success Response (200)
- **deviceType** (Object) - The device type object matching the provided slug or name.
#### Response Example
```json
{
"id": 1,
"slug": "raspberry-pi",
"name": "Raspberry Pi",
"is_virtual": false,
"data": {
"is_hidden": false
}
}
```
## GET /deviceType.getName
### Description
Retrieves the human-readable display name for a given device type slug.
### Method
GET
### Endpoint
/deviceType.getName
### Parameters
#### Query Parameters
- **deviceTypeSlug** (String) - Required - The slug of the device type.
### Request Example
```javascript
balena.models.deviceType.getName('raspberry-pi').then(function(deviceTypeName) {
console.log(deviceTypeName);
});
```
### Response
#### Success Response (200)
- **deviceTypeName** (String) - The display name of the device type.
#### Response Example
```json
"Raspberry Pi"
```
## GET /deviceType.getSlugByName
### Description
Retrieves the unique slug for a given device type display name.
### Method
GET
### Endpoint
/deviceType.getSlugByName
### Parameters
#### Query Parameters
- **deviceTypeName** (String) - Required - The display name of the device type.
### Request Example
```javascript
balena.models.deviceType.getSlugByName('Raspberry Pi').then(function(deviceTypeSlug) {
console.log(deviceTypeSlug);
});
```
### Response
#### Success Response (200)
- **deviceTypeSlug** (String) - The slug of the device type.
#### Response Example
```json
"raspberry-pi"
```
## GET /deviceType.getInterpolatedPartials
### Description
Retrieves a device type contract with its partial templates resolved and interpolated.
### Method
GET
### Endpoint
/deviceType.getInterpolatedPartials
### Parameters
#### Query Parameters
- **deviceTypeSlug** (String) - Required - The slug of the device type.
### Request Example
```javascript
balena.models.deviceType.getInterpolatedPartials('raspberry-pi').then(function(contract) {
for (const partial in contract.partials) {
console.log(`${partial}: ${contract.partials[partial]}`);
}
});
```
### Response
#### Success Response (200)
- **contract** (Object) - The device type contract object with interpolated partials.
#### Response Example
```json
{
"name": "raspberry-pi",
"slug": "raspberry-pi",
"partials": {
"bootDevice": ["Connect power to the Raspberry Pi (v1 / Zero / Zero W)"]
}
}
```
## GET /deviceType.getInstructions
### Description
Retrieves step-by-step instructions for installing the host OS on a specified device type.
### Method
GET
### Endpoint
/deviceType.getInstructions
### Parameters
#### Query Parameters
- **deviceTypeSlugOrContract** (String | Object) - Required - The slug of the device type or the device type contract object.
### Request Example
```javascript
balena.models.deviceType.getInstructions('raspberry-pi').then(function(instructions) {
for (let instruction of instructions.values()) {
console.log(instruction);
}
});
```
### Response
#### Success Response (200)
- **instructions** (Object | String[]) - An object or array of strings representing the installation instructions.
#### Response Example
```json
[
"Insert the sdcard to the host machine.",
"Write the BalenaOS file you downloaded to the sdcard. We recommend using Etcher.",
"Wait for writing of BalenaOS to complete.",
"Remove the sdcard from the host machine.",
"Insert the freshly flashed sdcard into the Raspberry Pi (v1 / Zero / Zero W).",
"Connect power to the Raspberry Pi (v1 / Zero / Zero W) to boot the device."
]
```
## GET /deviceType.getInstallMethod
### Description
Retrieves the installation method supported for a given device type.
### Method
GET
### Endpoint
/deviceType.getInstallMethod
### Parameters
#### Query Parameters
- **deviceTypeSlug** (String) - Required - The slug of the device type.
### Request Example
```javascript
balena.models.deviceType.getInstallMethod('raspberry-pi').then(function(method) {
console.log(method);
});
```
### Response
#### Success Response (200)
- **method** (String) - The installation method string (e.g., 'externalBoot').
#### Response Example
```json
"externalBoot"
```
```
--------------------------------
### Two-Factor Authentication Management
Source: https://docs.balena.io/reference/sdk/node-sdk/latest/index
Manage two-factor authentication settings, including verification, setup, enabling, challenging, and disabling.
```APIDOC
## POST /auth/twoFactor/verify
### Description
Verifies the two-factor authentication code. Note: This method does not automatically update the token. Use `challenge` when possible, as it handles token updates.
### Method
POST
### Endpoint
/auth/twoFactor/verify
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
- **code** (String) - Required - The two-factor authentication code.
### Request Example
```javascript
const token = balena.auth.twoFactor.verify('1234');
balena.auth.loginWithToken(token);
```
### Response
#### Success Response (200)
- **sessionToken** (String) - The session token upon successful verification.
```
```APIDOC
## GET /auth/twoFactor/getSetupKey
### Description
Retrieves a setup key required for enabling two-factor authentication.
### Method
GET
### Endpoint
/auth/twoFactor/getSetupKey
### Parameters
None
### Request Example
```javascript
const setupKey = balena.auth.twoFactor.getSetupKey();
console.log(setupKey);
```
### Response
#### Success Response (200)
- **setupKey** (String) - The setup key for enabling 2FA.
```
```APIDOC
## POST /auth/twoFactor/enable
### Description
Enables two-factor authentication for the account.
### Method
POST
### Endpoint
/auth/twoFactor/enable
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
- **code** (String) - Required - The code provided during the 2FA setup process.
### Request Example
```javascript
const token = balena.auth.twoFactor.enable('1234');
balena.auth.loginWithToken(token);
```
### Response
#### Success Response (200)
- **sessionToken** (String) - The session token upon successful enablement.
```
```APIDOC
## POST /auth/twoFactor/challenge
### Description
Challenges two-factor authentication and completes the login process. It is recommended to use the `login` method when possible, as it handles token persistence.
### Method
POST
### Endpoint
/auth/twoFactor/challenge
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
- **code** (String) - Required - The two-factor authentication code.
### Request Example
```javascript
balena.auth.twoFactor.challenge('1234');
```
### Response
#### Success Response (200)
(No specific return value documented, implies completion of challenge.
```
```APIDOC
## POST /auth/twoFactor/disable
### Description
Disables two-factor authentication for the account.
### Method
POST
### Endpoint
/auth/twoFactor/disable
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
- **password** (String) - Required - The user's current password.
### Request Example
```javascript
const token = balena.auth.twoFactor.disable('1234');
balena.auth.loginWithToken(token);
```
### Response
#### Success Response (200)
- **sessionToken** (String) - The session token upon successful disabling.
```
--------------------------------
### Device Type API
Source: https://docs.balena.io/reference/sdk/node-sdk/latest/index
Provides methods for retrieving information about supported device types, including their capabilities and installation methods.
```APIDOC
## Device Type API
### Description
Methods for retrieving information about supported device types, including capabilities and installation methods.
### Methods
- **.get(idOrSlug, [options])**
- **Description**: Gets a specific device type by its ID or slug.
- **Returns**: `Promise