Tech Bool Tech Bool
Gumroad API v2 JavaScript client

Gumroad API v2 JavaScript client

December 9, 2024
5 min read
Table of Contents

If you’re a digital creator or entrepreneur, you’ve probably heard of Gumroad — a platform that makes selling products online seamless. While Gumroad provides an API to integrate its services into custom applications, working directly with APIs can sometimes feel tedious or overly complex.

That’s where this JavaScript client comes in! Designed to simplify your workflow, this client allows you to interact with Gumroad’s API effortlessly, whether you’re fetching product details, managing subscribers, or automating sales data. With clean syntax and practical methods, this client will save you time and make building on Gumroad a breeze.

In this post, I’ll guide you through what this client offers, how to set it up, and examples of how you can use it to supercharge your Gumroad integrations. Let’s dive in! 🔥

Installation

Install the package by running the following command in terminal:

npm install @deox/gumroad

Usage

Put your Gumroad access token token as GUMROAD_ACCESS_TOKEN in your .env file.

.env
GUMROAD_ACCESS_TOKEN = "your_gumroad_access_token"

The module can be imported using import in ES Modules and require in Common JS as shown below:

ES Modules:

src/index.ts
import { Gumroad } from "@deox/gumroad";
 
const gumroad = new Gumroad(process.env.GUMROAD_ACCESS_TOKEN);

Common JS:

src/index.cjs
const { Gumroad } = require("@deox/gumroad");
 
const gumroad = new Gumroad(process.env.GUMROAD_ACCESS_TOKEN);

API Reference

I have listed few of methods, although all the methods in Gumroad API v2 are available in this client. You can explore all of them by installing the package.

gumroad.products.list()

Returns: Promise<Product[]>

See: https://app.gumroad.com/api#get-/products

Retrieve all of the existing products for the authenticated user.

const products = await gumroad.products.list();

gumroad.products.get(product_id)

Parameters:

  • product_id: string - Id of the product

Returns: Promise<Product>

See: https://app.gumroad.com/api#get-/products/:id

Retrieve the details of a product.

const product_id = "xxxxxxxxx"
const product = await gumroad.products.get(product_id);

gumroad.products.delete(product_id)

Parameters:

  • product_id: string - The id of the product to delete

Returns: true

See: https://app.gumroad.com/api#delete-/products/:id

Permanently delete a product.

const product_id = "xxxxxxxxx"
try {
  await gumroad.products.delete(product_id);
} catch (e) {
  // failed to delete product
  console.log(e);
}

gumroad.products.enable(product_id)

Parameters:

  • product_id: string - The id of the product to enable

Returns: Promise<Product>

See: https://app.gumroad.com/api#put-/products/:id/enable

Enable an existing product.

const product_id = "xxxxxxxxx"
const enabledProduct = await gumroad.products.enable(product_id);

gumroad.products.disable(product_id)

Parameters:

  • product_id: string - The id of the product to disable

Returns: Promise<Product>

See: https://app.gumroad.com/api#put-/products/:id/disable

Disable an existing product.

const product_id = "xxxxxxxxx"
const disabledProduct = await gumroad.products.disable(product_id);

gumroad.licenses.verify(product_id, license_key, [increment_uses_count])

Parameters:

  • product_id: string - The unique ID of the product, available on product’s edit page
  • license_key: string - The license key provided by your customer
  • increment_uses_count: boolean - If true, increment the uses count of a license. Default: true

Returns: Promise<Purchase>

See: https://app.gumroad.com/api#post-/licenses/verify

Verify a license

const product_id = "xxxxxxxxx"
const license_key = "xxxxxxxx-xxxxxxxx-xxxxxxxx-xxxxxxxx"
const purchase = await gumroad.licenses.verify(product_id, license_key);

gumroad.licenses.enable(product_id, license_key)

Parameters:

  • product_id: string - The unique ID of the product, available on product’s edit page
  • license_key: string - The license key provided by your customer

Returns: Promise<Purchase>

See: https://app.gumroad.com/api#put-/licenses/enable

Enable a license

const product_id = "xxxxxxxxx"
const license_key = "xxxxxxxx-xxxxxxxx-xxxxxxxx-xxxxxxxx"
const enabledPurchase = await gumroad.licenses.enable(product_id, license_key);

gumroad.licenses.disable(product_id, license_key)

Parameters:

  • product_id: string - The unique ID of the product, available on product’s edit page
  • license_key: string - The license key provided by your customer

Returns: Promise<Purchase>

See: https://app.gumroad.com/api#put-/licenses/disable

Disable a license

const product_id = "xxxxxxxxx"
const license_key = "xxxxxxxx-xxxxxxxx-xxxxxxxx-xxxxxxxx"
const disabledPurchase = await gumroad.licenses.disable(product_id, license_key);

gumroad.licenses.decrementUsesCount(product_id, license_key)

Parameters:

  • product_id: string - The unique ID of the product, available on product’s edit page
  • license_key: string - The license key provided by your customer

Returns: Promise<Purchase>

See: https://app.gumroad.com/api#put-/licenses/decrement_uses_count

Decrement the uses count of a license

const product_id = "xxxxxxxxx"
const license_key = "xxxxxxxx-xxxxxxxx-xxxxxxxx-xxxxxxxx"
const purchase = await gumroad.licenses.decrementUsesCount(product_id, license_key);

Webhook (Resource Subscriptions)

You can handle webhook requests with ease by using this library. An example is shown below which is written for Cloudflare Workers:

src/index.ts
import { Gumroad } from "@deox/gumroad";
 
export type Env = Readonly<{
 GUMROAD_ACCESS_TOKEN: string;
}>;
 
const handleWebhook = async (request: Request, env: Env) => {
 const gumroad = new Gumroad(env.GUMROAD_ACCESS_TOKEN);
 
 gumroad.on(
  "ping",
  async (ctx, next) => {
   console.log(
    `Good news! A new sale was made using an email address ${ctx.data.email}`
   );
 
   const sale = await ctx.api.sales.get(ctx.data.sale_id);
 
   console.log(sale);
 
   // ...
 
   if (ctx.data.test) {
    await next();
   }
  },
  () => {
   // This will be called only on test pings
   console.log("A test ping!");
  }
 );
 
 // Second parameter is type of resource subscription
 // which is going to be posted on this endpoint
 // Make sure you specify the correct type
 return gumroad.handle(request, "ping");
};
 
export default {
 async fetch(request, env) {
  const url = new URL(request.url);
 
  // Check for pathname and request method
  if (request.method === "POST" && url.pathname === "/ping") {
   return handleWebhook(request, env);
  }
 
  return new Response(null, { status: 404 });
 }
} satisfies ExportedHandler<Env>;

Wrapping Up

This JavaScript client for the Gumroad API is built to make integrations simpler and more efficient. From managing products to automating workflows, it empowers you to create custom solutions without the usual complexities.

Start exploring its features today and unlock new possibilities for your Gumroad projects.

Happy coding! 🚀