Trying to provide a more idiomatic example, here’s library code which does the withdrawal:
async function doWithdrawal(currency: string, amount: string, address: string) {
const accounts = await client.apiClient.rest
.coinbaseRequest({
baseURL: 'https://api.coinbase.com/',
url: `/v2/accounts`,
method: 'get'
})
.catch((e) => e.response);
const account = accounts.data.data.find((account) => account.currency.name.toUpperCase() === currency);
console.log(account);
const withdrawal = await client.apiClient.rest
.coinbaseRequest({
baseURL: 'https://api.coinbase.com/',
url: `/v2/accounts/${account.id}/transactions`,
method: 'post',
data: {
type: 'send',
to: address,
currency: currency,
amount: amount
}
})
.catch((e) => e.response);
console.log(withdrawal.data);
}
Note client.apiClient.rest.coinbaseRequest
is just a simple wrapper around axios
which will handle security / headers.
Calling with:
await doWithdrawal('BONK', '100', '<address>');
await doWithdrawal('USDC', '1', '<address>');
They both successfully find an account, here’s the USDC one:
{
id: '<id>',
name: 'USDC Wallet',
primary: true,
type: 'wallet',
balance: { amount: '<balance>', currency: 'USDC' },
created_at: '2023-12-07T19:36:12Z',
updated_at: '2023-12-26T19:56:19Z',
resource: 'account',
resource_path: '/v2/accounts/<id>',
currency: {
asset_id: '2b92315d-eab7-5bef-84fa-089a131333f5',
code: 'USDC',
color: '#2775CA',
exponent: 6,
name: 'USDC',
slug: 'usdc',
type: 'crypto',
rewards: { apy: '0.051', formatted_apy: '5.10%', label: '5.10% APY' }
},
allow_deposits: true,
allow_withdrawals: true
}
And here’s the USDC error:
{
errors: [
{
id: 'validation_error',
message: 'Please enter a valid email or ethereum address',
field: 'base'
}
]
}