Sign Your Requests

Require an HTTP message signature on calls that move money, so a leaked API key alone cannot pay anyone.

Signed requests are optional and off unless you turn them on. While they are on, the endpoints that move money accept a call only if it carries a signature made with a private key that never leaves your servers. Anyone who gets hold of your API key, but not that private key, can still read data but cannot pay out.

Two endpoints check signatures:

Every other endpoint ignores signature headers, so you can sign every request or only those two.

Signatures follow RFC 9421, HTTP Message Signatures, with the restrictions described on this page.

Setting up signing

The setting is yours, not your business's. It covers the API keys you generated, and each teammate who generates keys sets it up for their own.

  1. Create a key pair on the server that makes the calls. Keep the private key there, and never share it with Duplo.
  2. In Duplo Dashboard, open Settings › Developer API, add the public key, and give it a key ID such as prod-server-1. The key ID is up to 64 characters of letters, digits, ., _, :, and -, and you send it with every signature.
  3. Turn on signed requests on the same page. You need at least one public key first.

Public keys belong to one mode, so add them separately for test and live. You can hold up to 10 in each mode, which lets you rotate: add the new key, move your servers onto it, then remove the old one. You cannot remove your last key while signed requests are on.

Duplo accepts RSA keys of 2048 to 8192 bits and EC keys on P-256 or P-384, pasted as a PEM public key or an X.509 certificate. For example, with OpenSSL:

openssl ecparam -name prime256v1 -genkey -noout -out duplo-signing-key.pem
openssl ec -in duplo-signing-key.pem -pubout -out duplo-signing-key.pub.pem

Paste the contents of duplo-signing-key.pub.pem into Duplo Dashboard.

Ship the signing code first

The setting takes effect immediately. Deploy code that signs your payment calls, then turn signed requests on, or unsigned calls start failing with 401.

What a signed request carries

A signed request has three extra headers:

Signature headers
Content-Digest: sha-256=:X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE=:
Signature-Input: sig1=("@method" "@authority" "@path" "content-digest");alg="ecdsa-p256-sha256";keyid="prod-server-1";created=1731400000
Signature: sig1=:<base64 signature>:
  • Content-Digest is the SHA-256 or SHA-512 digest of the request body, base64-encoded between colons. It covers the exact bytes you send, so serialize the body once and send that same string. With request encryption on, that is the encrypted envelope.
  • Signature-Input lists what the signature covers and how it was made. It must cover @method, @authority, and @path, plus content-digest whenever the request has a body, and carry alg, keyid, and created. expires is optional.
  • Signature holds one base64-encoded signature, under the same label as Signature-Input. Send exactly one signature per request.

alg must match your key:

Keyalg
RSArsa-pss-sha512 or rsa-v1_5-sha256
EC P-256ecdsa-p256-sha256
EC P-384ecdsa-p384-sha384

created is the Unix time in seconds when you signed, and it must be within 5 minutes of Duplo's clock, so sign each request just before you send it.

Building the signature

The signature is over a signature base: one line for each covered component, in the order Signature-Input lists them, then the signature parameters. For the example headers it is:

Signature base
"@method": POST
"@authority": api.example.com
"@path": /api/v1/expenses/98d34e0c-69dc-4683-9bbe-6e6921ed155c/pay
"content-digest": sha-256=:X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE=:
"@signature-params": ("@method" "@authority" "@path" "content-digest");alg="ecdsa-p256-sha256";keyid="prod-server-1";created=1731400000
  • @method is the HTTP method in capitals.
  • @authority is the Host you send, in lowercase, including a port if your base URL has one.
  • @path is the path without the query string, starting at /api/v1.
  • @signature-params repeats everything in Signature-Input after sig1=, character for character.

Lines are separated by a single newline, with none after the last line. ECDSA signatures are the raw r and s values joined together, not DER, and RSA-PSS uses a 64-byte salt.

The examples below sign the pay endpoint, one with an EC P-256 key and one with an RSA key:

const crypto = require("crypto");
const fs = require("fs");
const axios = require("axios");

const privateKey = crypto.createPrivateKey(fs.readFileSync("duplo-signing-key.pem"));
const keyId = "prod-server-1";

const signedHeaders = (method, url, body) => {
  const { host, pathname } = new URL(url);
  const digest = `sha-256=:${crypto.createHash("sha256").update(body).digest("base64")}:`;
  const created = Math.floor(Date.now() / 1000);
  const params = `("@method" "@authority" "@path" "content-digest");alg="ecdsa-p256-sha256";keyid="${keyId}";created=${created}`;
  const base = [
    `"@method": ${method.toUpperCase()}`,
    `"@authority": ${host.toLowerCase()}`,
    `"@path": ${pathname}`,
    `"content-digest": ${digest}`,
    `"@signature-params": ${params}`,
  ].join("\n");
  const signature = crypto.sign("sha256", Buffer.from(base), {
    key: privateKey,
    dsaEncoding: "ieee-p1363",
  });

  return {
    "Content-Digest": digest,
    "Signature-Input": `sig1=${params}`,
    Signature: `sig1=:${signature.toString("base64")}:`,
  };
};

const payExpense = async (expenseId, walletId) => {
  const url = `https://spend-api.tryduplo.com/api/v1/expenses/${expenseId}/pay`;
  const body = JSON.stringify({ walletId });

  const response = await axios.post(url, body, {
    headers: {
      Authorization: "Bearer <your-api-key>",
      "Content-Type": "application/json",
      ...signedHeaders("POST", url, body),
    },
  });

  console.log(response.data);
};

payExpense("98d34e0c-69dc-4683-9bbe-6e6921ed155c", "123e4567-e89b-12d3-a456-426614174000");

When a signature is rejected

A missing or failing signature returns 401 with Invalid request signature: followed by the reason:

ReasonWhat to check
Missing Signature or Signature-Input headerSigned requests are on for the key's owner, so the call must be signed
Signature-Input must include alg, keyid and createdAll three parameters are present, with alg and keyid in double quotes
Unsupported signature algorithmalg is one of the four values in the algorithm table
Send exactly one signatureSignature and Signature-Input each carry one entry
Content-Digest must use sha-256 or sha-512The body has a Content-Digest header using one of those two
Content-Digest does not match the request bodyThe digest was computed over the exact bytes sent, after any encryption
Signature is too old or from the futurecreated is within 5 minutes of the current time, and your server clock is right
Signature has expiredexpires, if you send it, is still in the future
Signature must cover @method, @authority, @path and content-digestThe covered components include all four, or the first three for a request with no body
Unknown keyidThe key ID matches a public key you added, in the same mode as the API key
Algorithm does not match the keyalg suits the key's type and curve
Signature does not verifyThe signature base is built exactly as described on this page, and the private key matches the public key you added

How is this guide?

Last updated on

On this page

Need help? Visit our help center