Skip to content

About

Node.js library for the Bandcamp API

Topics

Resources

Stars

12 stars

Watchers

2 watching

Forks

Repository files navigation

@nutriot/bandcamp-api

Library for interacting with the Bandcamp API

License Version: npm Version: jsr Build: NodeJS Build: Deno

Installation

On NodeJS or Bun you can install from npm

npm install --save @nutriot/bandcamp-api

On Deno you can install using JSR

deno add jsr:@nutriot/bandcamp-api

Prerequisites

In order to make API calls, you need to register to get client ID and secret.

Usage

Caution

You should never use this library in the browser to avoid leaking your API credentials!

Import and initialize the Bandcamp module

import Bandcamp from '@nutriot/bandcamp-api';

const api = new Bandcamp({
	id: '<YOUR_CLIENT_ID>',
	secret: '<YOUR_CLIENT_SECRET>',
});

Options

fetch

Default: the global fetch

Makes every request instead of the global fetch, for example to add logging or retries, or to send requests through a proxy:

const api = new Bandcamp({ id: '<YOUR_CLIENT_ID>', secret: '<YOUR_CLIENT_SECRET>', fetch: customFetch });

Methods

getClientCredentials()

Usage: getClientCredentials()

Returns access token and refresh token. The access token expires after one hour; use the refresh token to get a new one.

Example
const { access_token, refresh_token } = await api.getClientCredentials();

refreshToken()

Usage: refreshToken(refreshToken)

Access tokens expire after one hour. You can use the refresh token to get a new access token.

Example
await api.refreshToken(refresh_token);

getMyBands()

Usage: getMyBands(accessToken)

Returns a list of the bands you have access to (either through artist accounts, label accounts, or partnerships).

Example
await api.getMyBands(access_token);

getSalesReport()

Usage: getSalesReport(accessToken, requestBody, version?)

Returns your sales reports for a label, band, or artist

Important

This method uses version 4 of the sales report API by default, which is a breaking change:

  • The result is { report: [...] }, an array of line items, instead of an object keyed by item ID
  • Each line item adds bandcamp_transaction_item_id, bandcamp_related_transaction_id, additional_fan_contribution, tax_rate and collection_society_share
  • The format option is deprecated. v4 only returns JSON. CSV isn't supported on any version, and a CSV response resolves to { error: true, message }

To keep using an older version, pin it explicitly, e.g. getSalesReport(access_token, body, 2). Pinned versions return their own, untyped shape.

Example
await api.getSalesReport(access_token, {
	band_id: 1633770804,
	member_band_id: 1925197437,
	start_time: '2015-12-31 23:59:59',
	end_time: '2016-01-31 00:00:00',
});

generateSalesReport()

Usage: generateSalesReport(accessToken, requestBody, version?)

Starts generating a sales report in the background and returns { token }. Takes the same parameters as getSalesReport(), plus format ("csv" or "json", default "json"). Use this instead of getSalesReport() for large reports or CSV. Defaults to version 4; pass 2 to use version 2.

fetchSalesReport()

Usage: fetchSalesReport(accessToken, { token }, version?)

Returns { url } to download a report started with generateSalesReport(). Resolves to { error: true, message } when the token is invalid or the report hasn't been generated yet. Defaults to version 4; pass 2 to use version 2.

Example
const generated = await api.generateSalesReport(access_token, {
	band_id: 1633770804,
	start_time: '2021-01-01 00:00:00',
	end_time: '2021-02-01 00:00:00',
	format: 'csv',
});

if ('error' in generated) throw new Error(generated.message);

// poll until the report is ready
let report = await api.fetchSalesReport(access_token, { token: generated.token });

while ('error' in report && report.message === "Report hasn't generated yet") {
	await new Promise((resolve) => setTimeout(resolve, 5000));
	report = await api.fetchSalesReport(access_token, { token: generated.token });
}

if ('error' in report) throw new Error(report.message);

const csv = await (await fetch(report.url)).text();

getMerchDetails()

Usage: getMerchDetails(accessToken, requestBody)

Returns merchandise a label, band, or artist has available for purchase on Bandcamp

Example
await api.getMerchDetails(access_token, {
	band_id: 1633770804,
	start_time: '2015-12-31',
	end_time: '2016-01-01',
	member_band_id: 1925197437,
	package_ids: [175167691, 1154611570],
});

getShippingOriginDetails()

Usage: getShippingOriginDetails(accessToken, requestBody)

Returns the shipping origins for artists and labels linked to your account on Bandcamp

Example
await api.getShippingOriginDetails(access_token);

getOrders()

Usage: getOrders(accessToken, requestBody, version?)

Returns merchandise orders placed with a band or label

Note

This method uses version 4 by default, which reports failed PayPal eChecks as failed instead of paid. Pass 3 to pin the previous version. CSV isn't supported, and format: "csv" resolves to { error: true, message }.

Example
await api.getOrders(access_token, {
	band_id: 1633770804,
});

updateShipped()

Usage: updateShipped(accessToken, itemsArray)

Updates shipped/unshipped status of merchandise orders

Example
await api.updateShipped(access_token, [
	{
		id: 1925197437,
		id_type: 'p',
		shipped: true,
		notification_message: 'Your items have shipped!',
		ship_date: '2016-02-29 12:59:59',
		carrier: 'UPS',
		tracking_code: 'VM13243546US',
	},
	{
		id: 4261657553,
		id_type: 's',
		shipped: false,
	},
]);

markDateRangeAsShipped()

Usage: markDateRangeAsShipped(accessToken, requestBody)

Updates shipped/unshipped status of merchandise orders within given date range

Example
await api.markDateRangeAsShipped(access_token, {
	band_id: 2293737955,
	member_band_id: 4261657553,
	start_time: '2015-12-31 23:59:59',
	end_time: '2016-01-31 00:00:00',
	email_notifications: true,
});

updateQuantities()

Usage: updateQuantities(accessToken, itemsArray)

Updates merch items' stock quantities (inventory levels)

Note: Because of the inherent race condition, this method requires you pass in a quantity_sold parameter as well as quantity_available.

Example
await api.updateQuantities(access_token, [
	{
		id_type: 'p',
		id: 3387163565,
		quantity_available: 365,
		quantity_sold: 57,
		origin_id: 12345698,
	},
	{
		id_type: 'o',
		id: 6789054322,
		quantity_available: 45,
		quantity_sold: 12,
		origin_id: 12345678,
	},
]);

updateSKU()

Usage: updateSKU(accessToken, itemsArray)

Updates merch item stock-keeping unit (SKU)

Example
await api.updateSKU(access_token, [
	{
		id: 175167691,
		id_type: 'p',
		sku: 'AFIB',
	},
	{
		id: 1154611570,
		id_type: 'o',
		sku: 'AFIB-XL',
	},
]);

Validators

This library includes a set of experimental validators for the API. Since the responses are undocumented at this point, the validators are not guaranteed to be correct (PRs are welcome!)

Future versions of this library will create its types from these validator schemas.

Valibot

import * as validators from '@nutriot/bandcamp-api/schema/valibot';

Zod

import * as validators from '@nutriot/bandcamp-api/schema/zod';

License

This work is licensed under The MIT License.

About

Node.js library for the Bandcamp API

Topics

Resources

Stars

12 stars

Watchers

2 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages