Created
July 24, 2015 22:03
-
-
Save eduardo/834d05e9ce80790bc143 to your computer and use it in GitHub Desktop.
Calling the Coinbase v2 API from Perl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.