Interacting with the Coinbase API only for your Coinbase account
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
3. Choose the appropriate API permissions as listed here.
API_KEY
and API_SECRET
. Keep them handy.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);
}
});