Utilizing the Coinbase API in a Node.js Application | by Jennifer Bland | Jan, 2022

Get a list of all your cryptocurrency transactions

Jennifer Bland
Photo by Kelly Sekema on Unsplash
let accessToken = ""; let refreshToken = "";
try { 
const response = await axios(config);
// saving tokens for other requests
accessToken = response.data.access_token;
refreshToken = response.data.refresh_token;
res.send({ response: response?.data });
}
// Gets the user details 
router.get("/user", async (req, res) => {
const config = {
method: 'get',
url: 'https://api.coinbase.com/v2/user',
headers: {
'Authorization': `Bearer ${accessToken}`,
'CB-VERSION': '2021-06-23'
}
};
try {
const response = await axios(config);
res.send({ response: response?.data })
} catch (e) {
console.log("Could not get user", e.response.data)
}
});
// Gets all account details 
router.get("/accounts", async (req, res) => {
const config = {
method: 'get',
url: 'https://api.coinbase.com/v2/accounts',
headers: {
'Authorization': `Bearer ${accessToken}`,
'CB-VERSION': '2021-06-23'
}
};
try {
const response = await axios(config);
res.send({ response: response?.data })
} catch (e) {
console.log("Could not get accounts", e.response.data)
}
});
try {
const response = await axios(config);
// filter out only accounts that have a balance
let accounts = response.data.data.filter( item => {
if (parseFloat(item.balance.amount) > 0 ) {
return item;
}
});
res.send({ response: accounts })
} catch (e) {
console.log("Could not get accounts", e.response.data)
}

and the button in the file body Tag leave the title only. Add the following code below the title:

<table>
<thead>
<tr>
<th>Wallet Name</th>
<th>Currency</th>
<th>Balance</th>
<th></th>
</tr>
</thead>
<tbody>
<% for(var i=0; i< accounts.length; i++){ %>
<tr>
<td><%= accounts[i].name %></td>
<td><%= accounts[i].currency.name %></td>
<td><%= accounts[i].balance.amount %></td>
<td><a href='<%= '/transactions/' + accounts[i].id %>' class="btn">Get Transactions</a></td>
</tr>
<% } %>
</tbody>
</table>
res.render('accounts', { title: 'Accounts', accounts: accounts });
table { 
color: #333;
background: white;
border: 1px solid grey;
font-size: 12pt;
border-collapse: collapse;
margin-bottom: 50px;
}
table thead th,
table tfoot th {
color: #777;
background: rgba(0,0,0,.1);
}
table caption {
padding:.5em;
}
table th,
table td {
padding: .5em;
border: 1px solid lightgrey;
}
res.redirect('/accounts');
router.get('/transactions/:id', async(req, res) => { 
const { id } = req.params;
const config = {
method: 'get',
url: `https://api.coinbase.com/v2/accounts/${id}/transactions`,
headers: {
'Authorization': `Bearer ${accessToken}`,
'CB-VERSION': '2021-06-23'
}
};
try {
const response = await axios(config);
res.send({ response: response?.data })
} catch (e) {
console.log("Could not get user authentication details", e.response.data)
}
});
<table> 
<thead>
<tr>
<th>Type</th>
<th># of Crypto</th>
<th>Amount</th> <th>Date</th>
</tr>
</thead>
<tbody>
<% for(var i=0; i< transactions.length; i++){ %>
<tr>
<td><%= transactions[i].details.title %></td>
<td><%= transactions[i].amount.amount %></td>
<td><%= transactions[i].native_amount.amount %></td>
<td><%= new Intl.DateTimeFormat().format(new Date(transactions[i]).created_at) %></td>
</tr>
<% } %>
</tbody>
</table>
res.render('transactions', { 
title: 'Transactions',
transactions: response?.data.data
});

Leave a Comment