Step 2: Create NFT Order

Create your first NFT order

You can start the order process with Banxa with the Create NFT Order request.

Code example below.

const axios = require('axios').default;

function generateHmac(data, nonce) {
    ...
 }
 
function sendPostRequest(query, payload) {
    const hostname = 'api.banxa-sandbox.com';
    const nonce = Date.now();
    let data = "POST\n" + query + "\n" + nonce + "\n" + payload;

    const authHeader = generateHmac(data, nonce);
    axios.post({
        method: "post",
        url: hostname + query,
        data: payload,
        headers: {
            'Authorization': 'Bearer ' + authHeader,
            'Content-Type': 'application/json'
        }
    }).then((response) => {
        console.log(response);
    }).catch((error) => {
        console.error(error);
    })
}

sendPostRequest('/api/orders/nft/buy', '{"account_reference": "testxxxx-1", "source": "USD", "source_amount": "150", "target": "ETH", "blockchain": "ERC20", "return_url_on_success": "http://test.com", "wallet_address": "{VALID_WALLET_ADDRESS}", "meta_data": { "purchase_reference": "XXX", "nft": {"contract_address: "0x0000000000", "token_id": "1", "name": "Banxa nft", "collection": "Banxa NFT shop", "media": { "type": "image", "link": "https://ipfs.example.cloud/ipfs/QmTWyo67Vd4GNVkbBCs96ddXnZZEJy41Cjc9DkcQRt41Np"}}}}')
import requests
import time
import hmac

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

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

    data = 'POST\n' + query + '\n' + str(nonce) + '\n' + payload

    authHeader = generateHmac(data, nonce)

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

    print(response.content)

sendPostRequest('/api/orders', '{"account_reference":"test01","source":"USD","target":"BTC","source_amount":"100","return_url_on_success":"test.com","wallet_address":"[VALID_WALLET_ADDRESS]"}')
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.sendPostRequest("/api/orders", "{" +
                "\"account_reference\":\"test01\"," +
                "\"source\":\"USD\"," +
                "\"target\":\"BTC\"," +
                "\"source_amount\":\"100\"," +
                "\"return_url_on_success\":\"test.com\"," +
                "\"wallet_address\":\"[VALID_WALLET_ADDRESS]\""+
                "}");
    }

    private void sendPostRequest(String query, String payload) throws Exception {
        String hmac = getHmac("POST", query, payload);

        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")
                .POST(HttpRequest.BodyPublishers.ofString(payload))
                .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 {
        ...
    }
}

This can also be put into a WebView if you are integrating Banxa into your app.

If you would like to host our checkout forms as a widget on your site, then you can use the checkout_iframe field.

In order to receive this you need to send us the iframe_domain in your Create NFT Order request.
More details can be found in Checkout Approaches.

Key things to remember

The JSON string should not contain any whitespace outside of double quotes