Developer docs

Build your first Dispense Flow integration in 15 minutes

· Dispense Flow developers

In short

Create a test store and an app in the developer dashboard, run a small Express server with a webhook route that verifies X-DFlow-Signature and an OAuth callback that exchanges the code, expose it with a tunnel, install the app on the test store and send a sample order.paid. Fifteen minutes, start to finish.

What you need

An approved developer account, Node 20, and a tunnel (ngrok, Cloudflare Tunnel) so the platform can reach your laptop over https.

1. The server

One file: a webhook route that keeps the raw body, and an OAuth callback.

import crypto from "node:crypto";
import express from "express";

const { DFLOW_CLIENT_ID, DFLOW_CLIENT_SECRET, DFLOW_WEBHOOK_SECRET } = process.env;
const API = "https://api.dispenseflow.com/v1";
const tokens = new Map(); // store_id -> access token (use a database)
const app = express();

app.post("/dflow/webhooks", express.raw({ type: "application/json" }), (req, res) => {
  const parts = Object.fromEntries((req.get("x-dflow-signature") ?? "").split(",").map((kv) => kv.split("=")));
  const t = Number(parts.t);
  const expected = crypto.createHmac("sha256", DFLOW_WEBHOOK_SECRET).update(`${t}.${req.body}`).digest("hex");
  const fresh = Math.abs(Date.now() / 1000 - t) <= 300;
  if (!fresh || !parts.v1 || expected.length !== parts.v1.length ||
      !crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(parts.v1))) return res.sendStatus(400);
  const event = JSON.parse(req.body);
  console.log(event.type, event.merchantId, event.data);
  res.sendStatus(204);
});

app.get("/dflow/callback", async (req, res) => {
  // Check req.query.state against the session first.
  const r = await fetch(`${API}/oauth/token`, {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify({ grant_type: "authorization_code", client_id: DFLOW_CLIENT_ID,
      client_secret: DFLOW_CLIENT_SECRET, code: req.query.code }),
  });
  const body = await r.json();
  tokens.set(body.store_id, body.access_token);
  res.send(`Connected to ${body.store.name}`);
});

app.listen(3000);

2. The app

In the dashboard, create an app with the scopes read_orders and read_customers, your tunnel URL plus /dflow/webhooks as the webhook URL, the events order.paid and customer.created, and your tunnel URL plus /dflow/callback as the redirect URL. Copy the three secrets into your environment.

3. Install and fire an event

Create a test store, install the app on it, and send order.paid. Your terminal prints the event. Use the access token the dashboard showed to call the API:

curl https://api.dispenseflow.com/v1/app-api/customers -H "Authorization: Bearer dfapp_..."

4. Try the real install flow

Open the consent URL from your app's page in a browser where you are signed in to a store, approve, and watch your callback store the token. That is exactly what merchants will do.

Questions

Why is the raw body needed?

The signature covers the exact bytes we sent. Parsing and re-serialising JSON can change them, so verify first and parse after.