Coinbase API connectivity problem

Im trying to connecto to the Coinbase sandbox API. Ive done all the preliminary setup with the API keys and passphrase but I keep getting a 401 error. my code is below. Does anyone have an insights as to what I might be doing wrong here?
API_Secret = API_SECRET;
passphrase = PASSPHRASE;
API_KEY = API_KEY;
timestamp = num2str(round(posixtime(datetime())*1000));
message = [timestamp 'GET' '/accounts'];
signature = matlab.net.base64encode(HMAC(API_Secret, message,'SHA-256'));
url = 'https://api-public.sandbox.exchange.coinbase.com/accounts';
options = weboptions('ContentType', 'json', 'RequestMethod', 'get', ...
'HeaderFields', {'CB-ACCESS-KEY', API_KEY}, ...
'HeaderFields', {'CB-ACCESS-SIGN', signature}, ...
'HeaderFields', {'CB-ACCESS-TIMESTAMP', timestamp}, ...
'HeaderFields', {'CB-ACCESS-PASSPHRASE', passphrase});
response = webread(url, options);
data = jsondecode(response);

Answers (1)

Hi @Xymbu,
I understand that you are encountering 401 error,when trying to connect with Coinbase API.
  • The 401 error typically indicates Unauthorized error.So,it is important to verify that the API URL and credentials you are using are correct and match the information provided by the API source.
  • If everything checks out,then I would recommend defining a single structure for HeaderFields option as follows
header={
'CB-ACCESS-KEY', API_KEY;
'CB-ACCESS-SIGN', signature;
'CB-ACCESS-TIMESTAMP', timestamp;
'CB-ACCESS-PASSPHRASE', passphrase}
%or
header=struct('CB-ACCESS-KEY', API_KEY, 'CB-ACCESS-SIGN', signature, 'CB-ACCESS-TIMESTAMP', timestamp, 'CB-ACCESS-PASSPHRASE', passphrase, 'Content-Type', 'application/json');
  • And, it is important noting that the syntax of ContentType option is different with different versions,so it is worth trying Content-Type inplace of ContentType.
I hope this resolves the issue,
Thanks and Regard,
Venkat Siddarth V.

Products

Release

R2022b

Asked:

on 25 Jan 2023

Edited:

on 9 Mar 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!