Step 4: First API Call

To test out the authentication first try it with a simple GET request such as Get Crypto Currencies or Get Payment Methods.

import requests
import time
import hmac

url = 'https://[PARTNER-NAME].banxa-sandbox.com'

def generateHmac(payload, nonce):
   ...
    
def sendGetRequest(query):
    nonce = int(time.time())

    data = 'GET\n' + query + '\n' + str(nonce)

    authHeader = generateHmac(data, nonce)

    response = requests.get(url + query,
        headers = {
                'Authorization': 'Bearer ' + authHeader,
                'Content-Type': 'application/json'
        })

    print(response.content)

sendGetRequest('/api/payment-methods?source=USD')
package com.banxa;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.Formatter;

public class BanxaService {
    private static final String BANXA_URL = "https://api.banxa-sandbox.com";

    public static void main(String[] args) throws Exception {
        BanxaService banxaService = new BanxaService();
        banxaService.sendGetRequest("/api/payment-methods?source=USD");
    }

    private void sendGetRequest(String query) throws Exception {
        String hmac = getHmac("GET", query, null);

        HttpClient httpClient = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(BANXA_URL + query))
                .timeout(Duration.ofSeconds(10))
                .header("Authorization", "Bearer " + hmac)
                .header("content-type", "application/json")
                .GET()
                .build();

        HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());

        if (response.statusCode() == 200) {
            System.out.println("OK");
        } else {
            System.out.println("Failed: " + response.statusCode());
        }
    }

    public String getHmac(String method, String query, String payload) throws Exception {
        ...
    }
}
function generateHmac(data, nonce) {
   ...
}

function sendGetRequest(query) {
const hostname = 'api.banxa-sandbox.com';
const nonce = Date.now();
const method = 'GET'
let data = method + "\n" + query + "\n" + nonce;

const hmac = generateHmac(data, nonce);
const https = require('https')
const options = {
    hostname: hostname,
    path: query,
    method: method,
    headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${hmac}`
    }
}

const req = https.get(options, (res) => {
    console.log(`STATUS: ${res.statusCode}`);
    console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
    res.setEncoding('utf8');
    res.on('data', (chunk) => {
      console.log(`BODY: ${chunk}`);
    });
    res.on('end', () => {
      console.log('No more data in response.');
    });
  });
  
  req.on('error', (e) => {
    console.error(`problem with request: ${e.message}`);
  });
}

Key things to remember:

  • When generating the data for the HMAC algorithm include the query string