Last active
March 2, 2022 14:52
-
-
Save bdcorps/64b1258ad4670e7e39f0dd348cdd0a34 to your computer and use it in GitHub Desktop.
API Documentation - Coinbase for Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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