Bitbucket.js API Client

2.12.0 · active · verified Tue Apr 21

bitbucket (Bitbucket.js) is a comprehensive JavaScript API client for interacting with the Bitbucket Cloud API, designed for both Node.js and browser environments. The current stable version is 2.12.0. This library aims to provide a robust and type-safe interface, shipping with TypeScript definitions, making it well-suited for modern JavaScript and TypeScript projects. It supports various authentication methods, including username/password and token-based authentication, and provides access to numerous Bitbucket API namespaces such as repositories, pull requests, pipelines, and users. The project draws heavy inspiration from `octokit/rest.js`, suggesting a similar, well-structured, and idiomatic approach to API interactions. While no explicit release cadence is stated, it appears to be actively maintained, with regular updates to reflect the latest Bitbucket API changes and bug fixes. Its key differentiators include its dual Node.js/browser support, extensive API coverage, and strong typing, simplifying complex API interactions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Bitbucket client with token authentication and list repositories for the authenticated user ('me'). It includes error handling for common API issues like authentication failures.

import { Bitbucket } from 'bitbucket';

const BITBUCKET_ACCESS_TOKEN = process.env.BITBUCKET_ACCESS_TOKEN ?? '';

if (!BITBUCKET_ACCESS_TOKEN) {
  console.error('Error: BITBUCKET_ACCESS_TOKEN environment variable is not set.');
  process.exit(1);
}

const clientOptions = {
  baseUrl: 'https://api.bitbucket.org/2.0',
  auth: {
    token: BITBUCKET_ACCESS_TOKEN,
  },
  request: {
    timeout: 10000, // 10 seconds
  },
};

const bitbucket = new Bitbucket(clientOptions);

async function listMyRepositories() {
  try {
    console.log('Fetching repositories...');
    const { data } = await bitbucket.repositories.listForUser({ username: 'me' });
    if (data && data.values) {
      console.log('Found the following repositories:');
      data.values.forEach(repo => console.log(`- ${repo.name} (${repo.full_name})`));
    } else {
      console.log('No repositories found or unexpected response format.');
    }
  } catch (err) {
    if (err.status === 401 || err.status === 403) {
      console.error('Authentication failed. Check your BITBUCKET_ACCESS_TOKEN and permissions.');
    } else {
      console.error(`Error listing repositories: ${err.message}`);
      if (err.error) {
        console.error('API Error details:', err.error);
      }
    }
  }
}

listMyRepositories();

view raw JSON →