Balena HTTP Client

14.2.0 · active · verified Wed Apr 22

balena-request is a low-level HTTP client specifically designed for making requests to Balena servers. It is not intended for direct use by end-users, but rather as an internal dependency for other Balena-io modules, such as the balena SDK. The current stable version is 14.2.0, with frequent patch and minor releases, indicating active development. Key differentiators include its tight integration with the Balena ecosystem, requiring an instantiated `balena-auth` instance for authentication, and providing features like request interceptors, streaming capabilities, and automatic handling of authorization. It supports both Node.js (requiring `>=18.0.0`) and browser environments, though the `isBrowser` option has been deprecated in recent versions. The module exposes a factory function to create a request instance, enabling configurable behavior like debug logging and retries.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import `balena-request`, create a client instance with a mocked `balena-auth` dependency, and execute a simple GET request to the Balena API's `/ping` endpoint.

import balenaRequest from 'balena-request';
import balenaAuth from 'balena-auth';

// Mock balenaAuth for demonstration purposes.
// In a real application, you would instantiate and configure balenaAuth.
const auth = balenaAuth.createInstance({
  dataDirectory: process.env.BALENA_DATA_DIR ?? false, // Use false for in-memory in tests/examples
  // apiKey: process.env.BALENA_API_KEY ?? '', // Or use an API key
});

async function makeRequest() {
  const request = balenaRequest({
    auth: auth,
    debug: process.env.DEBUG_BALENA_REQUEST === 'true',
    // The `isBrowser` option is deprecated and no longer needed for modern environments.
    // isBrowser: false, 
  });

  try {
    console.log('Making a request to Balena API...');
    const result = await request.send({
      method: 'GET',
      url: 'https://api.balena-cloud.com/ping',
      headers: {
        'Content-Type': 'application/json',
      },
      // Note: balena-request automatically handles authorization if auth is provided
    });
    console.log('Request successful! Status:', result.statusCode);
    // console.log('Response body:', result.body);
  } catch (error) {
    console.error('Request failed:', error);
    if (error instanceof balenaRequest.BalenaRequestError) {
      console.error('This is a specific BalenaRequestError.');
    }
  }
}

makeRequest();

view raw JSON →