Skip to content

Instantly share code, notes, and snippets.

@eduardo
Created July 24, 2015 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eduardo/834d05e9ce80790bc143 to your computer and use it in GitHub Desktop.
Save eduardo/834d05e9ce80790bc143 to your computer and use it in GitHub Desktop.
Calling the Coinbase v2 API from Perl
#!/usr/bin/perl
use JSON;
use JSON::WebToken;
use Digest::SHA qw(hmac_sha256_hex);
my $apiKey= 'API_KEY';
my $apiSecret= 'API_SECRET';
my $urlcb = "https://api.coinbase.com/v2/orders";
my $url = URI->new($urlcb);
my $urlpath = $url->path();
my $nonce=time;
my %mas=(
"amount"=> "10.00",
"currency"=> "USD",
"name"=> "Order #123",
"description"=> "Sample order",
"metadata"=> {
"customer_id"=> "id_1005",
"customer_name"=> "Satoshi Nakamoto"
}
);
my $json = encode_json \%mas;
print "$json\n";
my $msg = $nonce.'POST'.$urlpath.$json;
print "$msg\n";
my $signature=hmac_sha256_hex($nonce.'POST'.$urlpath.$json, $apiSecret);
my %payload = (
"CB-ACCESS-KEY" => $apiKey,
"CB-ACCESS-TIMESTAMP" => $nonce,
"CB-ACCESS-SIGN" => $signature,
"CB-VERSION" => '2015-07-07'
);
use LWP::UserAgent;
my $ua=LWP::UserAgent->new;
my $req=HTTP::Request->new(POST=>$url);
$req->content_type('application/json');
$req->header(%payload);
$req->content($json);
#my $html = $ua->request($req)->as_string;
#print "$html";
#exit;
my $resp=$ua->request($req);
my $message=$resp->decoded_content;
use Data::Dumper;
$text = decode_json($message);
print Dumper($text);
@mardlin
Copy link

mardlin commented Jul 27, 2015

I don't really know from Perl. Am I correct that $urlpath is equivalent to /v2/orders here? If so, we should probably propose a clarification in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment