Skip to content

Instantly share code, notes, and snippets.

@bdcorps
Last active March 2, 2022 14:52
Show Gist options
  • Save bdcorps/64b1258ad4670e7e39f0dd348cdd0a34 to your computer and use it in GitHub Desktop.
Save bdcorps/64b1258ad4670e7e39f0dd348cdd0a34 to your computer and use it in GitHub Desktop.
API Documentation - Coinbase for Node.js
const express = require("express");
const axios = require('axios');
const crypto = require('crypto');
const app = express();
const API_KEY = "";
const API_SECRET = "";
app.use(express.json());
app.use(express.urlencoded({
extended: true
}));
app.set('view engine', 'ejs');
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);
}
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)
}
});
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)
}
});
var port = process.env.PORT || 3006;
app.listen(port, '0.0.0.0', function () {
console.log("Server starting on localhost:" + port);
});
{
"name": "coinbase-api-docs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node ."
},
"dependencies": {
"axios": "^0.24.0",
"crypto": "^1.0.1",
"ejs": "^3.1.6",
"express": "^4.17.1",
"nodemon": "^2.0.15"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment