image

Blog Home

How to Sign Blacksheep API Requests with Ed25519 in Node and Python

image

Harrison Dell

29 November 2026

7 mins

To sign API requests with Ed25519 for Blacksheep, you build a length-prefixed signing string from the request, sign its UTF-8 bytes with your private key and send the base64 result in a header. The Node and Python examples below produce the same signing string as the authentication documentation.

Key takeaways

  • Every Blacksheep API request carries an Ed25519 envelope signature, so there are no bearer tokens and the server never sees your signing key.
  • The signing string is length-prefixed, and you sign its UTF-8 bytes with your private key and send the base64 result in the Bs-Signature header.
  • Signature failures return HTTP 401 with invalid_signature, stale_request or replay_detected.

How do you sign Blacksheep API requests with Ed25519 in Node and Python?

Every public request carries an Ed25519 envelope signature, so there are no bearer tokens and the server never sees your signing key. The signature shows that the request came from the holder of your private key, that it is recent and that the body was not changed in transit.

The base URL is https://api.blacksheep.money, and every endpoint is called with POST and a JSON body. Endpoints that take no input are called with no body at all. The API reference lists the endpoints and the response envelopes.

What credentials do I need?

You create a key in the dashboard under Settings, then Dev space, by entering a name and a reason. The dashboard generates an Ed25519 keypair in your browser, registers the public key and shows you two values.

  • BSK_KEY_ID: the public identifier, sent in the Bs-Key-Id header on every request.
  • BSK_SIGNING_KEY: the PKCS#8 DER private key encoded as standard base64, used locally to sign and never transmitted.

The signing key is shown once and cannot be retrieved again. If you lose it, revoke the key and create a new one. Keep it in a secrets manager and load it into environment variables at runtime.

What does the request envelope contain?

Each request needs five headers, and the last is conditional.

  • Bs-Key-Id: the value of BSK_KEY_ID.
  • Bs-Timestamp: the current Unix time in seconds.
  • Bs-Nonce: 16 random bytes encoded as standard base64, unique per request.
  • Bs-Signature: the base64 Ed25519 signature over the signing string.
  • Content-Digest: sha-256=:<base64 sha256 of the body>:, required when there is a body and omitted entirely when there is not.

What is the signing string?

The signing string joins its parts with a colon, and every variable-length field is preceded by its length as a decimal integer. The length is the UTF-8 byte length of the field, and the prefixes exist so that a value containing a colon cannot be used to inject a different field.

{len(Bs-Key-Id)}:{Bs-Key-Id}:{len(Bs-Timestamp)}:{Bs-Timestamp}:{len(Bs-Nonce)}:{Bs-Nonce}:{METHOD}:{len(path-with-query)}:{path-with-query}:{Content-Digest}

The method is the uppercase HTTP method with no length prefix. The Content-Digest segment is the header value when there is a body, or an empty string when there is not, again with no length prefix. For a request with no body, the string therefore ends with a colon followed by nothing.

The path is the full path including any query string, exactly as sent, for example /v1/account.balance.getMany. You sign the UTF-8 bytes of the finished string with your private key and base64-encode the result.

How do I sign a request in Node.js?

This example uses only the built-in crypto module, so it needs no dependencies.

import crypto from "crypto";

function signRequest({ method, path, body, keyId, signingKeyBase64 }) {
  const timestamp = Math.floor(Date.now() / 1000).toString();
  const nonce = crypto.randomBytes(16).toString("base64");

  let contentDigest = "";
  if (body && body.length > 0) {
    const hash = crypto.createHash("sha256").update(body).digest("base64");
    contentDigest = `sha-256=:${hash}:`;
  }

  const signingString = [
    keyId.length, keyId,
    timestamp.length, timestamp,
    nonce.length, nonce,
    method.toUpperCase(),
    path.length, path,
    contentDigest,
  ].join(":");

  const privateKey = crypto.createPrivateKey({
    key: Buffer.from(signingKeyBase64, "base64"),
    format: "der",
    type: "pkcs8",
  });
  const signature = crypto
    .sign(null, Buffer.from(signingString, "utf8"), privateKey)
    .toString("base64");

  return {
    "Bs-Key-Id": keyId,
    "Bs-Timestamp": timestamp,
    "Bs-Nonce": nonce,
    "Bs-Signature": signature,
    ...(contentDigest ? { "Content-Digest": contentDigest } : {}),
  };
}

A call with no input, such as listing balances, passes body: null and sends no body.

const headers = signRequest({
  method: "POST",
  path: "/v1/account.balance.getMany",
  body: null,
  keyId: process.env.BSK_KEY_ID,
  signingKeyBase64: process.env.BSK_SIGNING_KEY,
});

const response = await fetch("https://api.blacksheep.money/v1/account.balance.getMany", {
  method: "POST",
  headers,
});

With an input, sign the exact bytes you send. Build the body once as a Buffer, sign it and send that same Buffer.

const body = Buffer.from(JSON.stringify({ id: "1d2b8e7a-..." }));
const headers = signRequest({
  method: "POST",
  path: "/v1/transaction.get",
  body,
  keyId: process.env.BSK_KEY_ID,
  signingKeyBase64: process.env.BSK_SIGNING_KEY,
});

const response = await fetch("https://api.blacksheep.money/v1/transaction.get", {
  method: "POST",
  headers: { ...headers, "Content-Type": "application/json" },
  body,
});

How do I sign a request in Python?

This version uses the cryptography package. It loads the same base64 PKCS#8 DER key and builds the same signing string, and it uses requests only to send the call.

import base64
import hashlib
import json
import os
import time

import requests
from cryptography.hazmat.primitives import serialization


def sign_request(method, path, body, key_id, signing_key_b64):
    timestamp = str(int(time.time()))
    nonce = base64.b64encode(os.urandom(16)).decode("ascii")

    content_digest = ""
    if body:
        digest = base64.b64encode(hashlib.sha256(body).digest()).decode("ascii")
        content_digest = f"sha-256=:{digest}:"

    def field(value):
        return f"{len(value.encode('utf-8'))}:{value}"

    signing_string = ":".join([
        field(key_id),
        field(timestamp),
        field(nonce),
        method.upper(),
        field(path),
        content_digest,
    ])

    private_key = serialization.load_der_private_key(
        base64.b64decode(signing_key_b64), password=None
    )
    signature = base64.b64encode(
        private_key.sign(signing_string.encode("utf-8"))
    ).decode("ascii")

    headers = {
        "Bs-Key-Id": key_id,
        "Bs-Timestamp": timestamp,
        "Bs-Nonce": nonce,
        "Bs-Signature": signature,
    }
    if content_digest:
        headers["Content-Digest"] = content_digest
    return headers

The calls mirror the Node examples. For a call with no body, send nothing. For a call with an input, serialise the JSON once to bytes, sign those bytes and send the same bytes.

KEY_ID = os.environ["BSK_KEY_ID"]
SIGNING_KEY = os.environ["BSK_SIGNING_KEY"]
BASE = "https://api.blacksheep.money"

# No input: no body, no Content-Digest
path = "/v1/account.balance.getMany"
headers = sign_request("POST", path, None, KEY_ID, SIGNING_KEY)
response = requests.post(BASE + path, headers=headers)

# With input: sign the exact bytes you send
path = "/v1/transaction.get"
body = json.dumps({"id": "1d2b8e7a-..."}).encode("utf-8")
headers = sign_request("POST", path, body, KEY_ID, SIGNING_KEY)
headers["Content-Type"] = "application/json"
response = requests.post(BASE + path, headers=headers, data=body)

What are the common signing pitfalls?

Most signature failures come from a small set of construction errors.

  • Missing length prefixes: every variable-length field (key ID, timestamp, nonce and path) needs its byte length in front of it, or the server returns invalid_signature.
  • base64url instead of base64: the nonce, signature and digest use standard base64 with +, / and =. The base64url variants fail verification.
  • A different path: sign the full path including the query string exactly as sent, because any encoding difference breaks the signature.
  • Whitespace: strip leading and trailing whitespace from header values before signing.
  • Re-serialised bodies: sign the same bytes you send. Serialising the JSON a second time can change key order or spacing and break the digest.
  • Clock skew: the timestamp must be within 300 seconds of the server clock, so keep your servers synced with NTP.
  • Reused nonces: generate 16 fresh random bytes for every request, because reusing a nonce inside the 10-minute window is rejected.

What do the 401 errors mean?

All signature failures return HTTP 401 with a flat body of the form { "error": "<code>" }. Endpoint-level errors use a richer { "error": { "code", "message" } } envelope instead, so handle the two shapes separately.

  • invalid_signature: a required header is missing, the signature is wrong, the key ID is unknown or revoked, the Content-Digest is absent when there is a body, or the body does not match the digest.
  • stale_request: the timestamp is more than 300 seconds from the server clock.
  • replay_detected: the same key ID and nonce were used within the 10-minute window.

When you debug, compare each field of your signing string byte for byte against the order and format above. An invalid_signature on an otherwise valid key almost always means the signing string was built incorrectly.

What should I build next?

Once signing works, the usual sequence is balances, then recipients, then quotes and transfers, then webhooks. The integration guide sets out that sequence and a plan for test payments, and the quotes documentation explains the single-use quote token. Webhook deliveries use a different scheme, signed by Blacksheep and verified by you with a public key, which the webhooks documentation describes.

If you want to see a signed request without writing code, the Playground signs in your browser with your key and shows the exact request and response. It runs against the live API. For the business side of the API, see payments API for crypto exchanges and how the Partner API works.

To get an API key or ask a question, contact us or create a business account.

Frequently asked questions

How do I sign API requests with Ed25519 in Node.js?

Build the length-prefixed signing string, then call crypto.sign(null, Buffer.from(signingString, "utf8"), privateKey) with a private key created from your base64 PKCS#8 DER value. Base64-encode the result and send it in the Bs-Signature header.

How do I sign API requests with Ed25519 in Python?

Load the key with serialization.load_der_private_key(base64.b64decode(key), password=None) from the cryptography package. Then call private_key.sign(signing_string.encode()) and base64-encode the signature.

Why do I get invalid_signature from the Blacksheep API?

The most common cause is a signing string that does not match the format above, such as a missing length prefix or a re-serialised body. Check that the key ID is active and that a Content-Digest is present whenever you send a body.

Does Blacksheep use API tokens or bearer tokens?

No. Every request is signed with your Ed25519 private key, which never leaves your systems. The server holds only the public key you registered in the dashboard.

Disclaimer

The information in this article is general in nature and does not constitute financial advice or a recommendation to buy, sell or hold digital assets. Fees shown are indicative and were accurate at the time of publication; final fees are displayed at the time of your transaction. Digital assets are volatile and you may lose some or all of the value you hold.

Blacksheep Operations Pty Ltd (ABN 16 674 111 392) is registered with AUSTRAC as a Digital Currency Exchange provider (DCE100860053-001) and as an Independent Remittance Dealer. Blacksheep is also registered on the New Zealand Financial Services Provider Register.

Enjoy easy buying and selling crypto in Australia

Blacksheep is fully registered Digital Currency Exchange (DCE) with AUSTRAC

Get Started

Share Post

imageimageimage

Begin Your Journey with Blacksheep

Join the community of Australian investors using Blacksheep today

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
imageimage