Gumroad

Retrieve all sales for a user.

G
import fetch from 'isomorphic-unfetch';

export default async (_, res) => {
  let url = '/v2/sales';
  const allSales = [];
  const API_KEY = process.env.GUMROAD_API_KEY;

  while (url) {
    const response = await fetch(`https://api.gumroad.com${url}`, {
      headers: {
        Authorization: `Bearer ${API_KEY}`,
        'Content-Type': 'application/json'
      },
      method: 'GET'
    });

    const { sales, next_page_url: nextPageUrl } = await response.json();

    allSales.push(sales);

    if (nextPageUrl) {
      url = nextPageUrl;
    } else {
      break;
    }
  }

  return res.status(200).json({ sales });
};
import fetch from 'isomorphic-unfetch';

exports.handler = async (event, context, callback) => {
  let url = '/v2/sales';
  const allSales = [];
  const API_KEY = process.env.GUMROAD_API_KEY;

  while (url) {
    const response = await fetch(`https://api.gumroad.com${url}`, {
      headers: {
        Authorization: `Bearer ${API_KEY}`,
        'Content-Type': 'application/json'
      },
      method: 'GET'
    });

    const { sales, next_page_url: nextPageUrl } = await response.json();

    allSales.push(sales);

    if (nextPageUrl) {
      url = nextPageUrl;
    } else {
      break;
    }
  }

  return {
    statusCode: 200,
    body: JSON.stringify({
      sales: allSales
    })
  };
};

Usage#

1

Create Gumroad Account

First, create a Gumroad account. Then, navigate to the Applications tab and create a new app.

2

Save Application Secret

Make note of your "Application Secret". Since we're communicating server to server, we only need the secret.

3

Add Environment Variable

To securely access the API, we need to include the secret with each request. We also do not want to commit secrets to git. Thus, we should use an environment variable.

  • Netlify: Settings -> Build & deploy -> Environment -> Environment variables.
  • Vercel: Settings -> General -> Environment Variables.

Alternatively, you can add the enviornment variable to your vercel.json or netlify.toml file.

New functions in your inbox

Get access to functions when they're added.