CircleCI API Client

0.2.4 · active · verified Wed Apr 22

The `circle-client` package provides a JavaScript and TypeScript client for interacting with the CircleCI v2 API. It allows developers to programmatically manage CI/CD pipelines, retrieve workflow and job insights, manage contexts and environment variables, and access user and project details. Currently at version `0.2.4`, the library is in a pre-1.0 development phase, which implies that minor versions may introduce breaking changes. It ships with comprehensive TypeScript definitions, offering a strongly-typed interface for API interactions, which significantly enhances developer experience and reduces common API-related errors. Key differentiators include its direct mapping of CircleCI v2 API endpoints to client methods, simplified handling of paginated results through a `Paged<T>` object, and a focus on abstracting the underlying HTTP request complexities, making it easier to integrate CircleCI operations into Node.js applications.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart initializes the CircleCI client using an API token and demonstrates fetching the current user's details and listing recent project pipelines. It highlights the use of `process.env` for secure token handling and shows how to interact with paginated results.

import CircleCI from 'circle-client';

// Get your CircleCI API token from https://app.circleci.com/settings/user/tokens
const CIRCLECI_API_TOKEN = process.env.CIRCLECI_API_TOKEN ?? 'YOUR_SECRET_TOKEN_HERE';

if (CIRCLECI_API_TOKEN === 'YOUR_SECRET_TOKEN_HERE') {
  console.warn('Please set the CIRCLECI_API_TOKEN environment variable or replace the placeholder.');
}

async function runExample() {
  try {
    const client = new CircleCI(CIRCLECI_API_TOKEN, {
      slug: ['github', 'jodyheavener', 'circle-client'], // Replace with your actual project slug
      branch: 'main',
    });

    console.log('Fetching current user details...');
    const me = await client.getMe();
    console.log('Logged in as:', me.name);

    console.log('Listing recent pipelines for the project...');
    const pipelinesPage = await client.listProjectPipelines({
      projectSlug: ['github', 'jodyheavener', 'circle-client'] // Required for this method
    });
    console.log(`Found ${pipelinesPage.items.length} pipelines. Latest:`, pipelinesPage.items[0]?.id);

    if (pipelinesPage.next_page_token) {
      console.log('More pipelines available. Next page token:', pipelinesPage.next_page_token);
    }

  } catch (error) {
    console.error('An error occurred:', error instanceof Error ? error.message : String(error));
  }
}

runExample();

view raw JSON →