API Reference: Coinbase using API Key (Node.js)

Featured Image

If you're looking to access Coinbase API on behalf of other users, head to the Coinbase using OAuth2 guide here.

Complete Source Code: https://gist.github.com/bdcorps/64b1258ad4670e7e39f0dd348cdd0a34

Getting Started

  • Create a new Coinbase account.

  1. Choose the appropriate API permissions as listed

here

.

  • After successfully creating the app, you will be presented with your API_KEY and API_SECRET. Keep them handy.

Make API Request

const createCBRequest = (method, url) => {
  var timeInSeconds = parseInt(new Date().getTime() / 1000);
  var sigString = timeInSeconds + `${method}${url}`;

  var hmac = crypto.createHmac("sha256", API_SECRET);
  signature = hmac.update(sigString).digest("hex");

  const config = {
    method,
    url: `https://api.coinbase.com${url}`,
    headers: {
      "CB-ACCESS-KEY": API_KEY,
      "CB-ACCESS-SIGN": signature,
      "CB-ACCESS-TIMESTAMP": timeInSeconds,
    },
  };

  return axios(config);
};

Get User Details

app.get("/user", async (req, res) => {
  try {
    const response = await createCBRequest("GET", "/v2/user");
    res.send({ response: response?.data });
  } catch (e) {
    console.log("Could not get user", e.response.data);
  }
});

Get Primary Account

app.get("/account", async (req, res) => {
  try {
    const response = await createCBRequest("GET", "/v2/accounts/BTC");
    res.send({ response: response?.data });
  } catch (e) {
    console.log("Could not get account", e.response.data);
  }
});

I'm building a new SaaS to automate content marketing for your SaaS

Check it out →

Tools for SaaS Devs