# Get paid by QR code

> Show a QR code any wallet can pay. The money arrives as the token you want.

- Status: live. Compiles on Composer today.
- Who it's for: Merchants and on-ramps
- Page: https://composer-experiences.li.quest/templates/qr-checkout
- Working example, run with your own key and wallet: https://composer-experiences.li.quest/templates/qr-checkout/live

## What you get

- Works with any wallet that can scan
- The payment is swapped and delivered for you

## What runs, in order

A step without ops is work outside the flow, such as the user paying in. `×n` marks an op the flow repeats.

1. Customer scans and pays (Base)
2. Lands in escrow (Base). Ops: `continuation.settle`
3. Delivered as your token (Base). Ops: `lifi.swap`

## What you control

| Parameter | Set by |
| --- | --- |
| Token you receive | Fixed by you |
| Where it lands | Fixed by you |

## Build it

1. Get an API key at https://portal.li.fi. Keep it on your server.
2. Install these exact versions; npm's `latest` tag lags production:

```sh
npm install @lifi/composer-sdk@0.16.0 @lifi/compose-spec@0.16.0
```

3. Add the module below as `qr-checkout.ts`. It names the flow `tpl:qr-checkout@1`, so LI.FI can see which template it came from.
4. Compile with `simulationPolicy: 'allow-revert'`. Before the user signs, show `outputs[<key>].amount.minimum.value`: the least they receive. It is optional; when it is absent, the flow has no floor.

### The module: `qr-checkout.ts`

```ts
/**
 * Get paid by QR code: the merchant compiles a deposit intent, and the response
 * carries a `fundEscrow` action anyone can send. Render it as a QR; once a
 * customer funds the escrow from any wallet, a solver swaps the payment to the
 * merchant's token and delivers at least the committed floor.
 *
 *   const { flow, run } = buildFlow(sdk, inputs);
 *   const result = await flow.compile(run); // result.actions holds fundEscrow
 */
import {
  type Address,
  type ComposeRunInput,
  type ComposeSdk,
  resources,
} from '@lifi/composer-sdk';

export interface QrCheckoutInputs {
  chainId: number;
  /** The ERC-20 the customer pays with. */
  payToken: Address;
  /** What the customer pays, in `payToken`'s smallest unit. */
  amount: bigint;
  merchantToken: Address;
  merchant: Address;
  slippageBps: number;
}

export const buildFlow = (
  sdk: ComposeSdk,
  inputs: QrCheckoutInputs,
  name = 'tpl:qr-checkout@1'
) => {
  const { chainId, payToken, merchantToken, merchant } = inputs;

  // The leg a solver runs from the funded escrow. The swap's slippage floor is
  // what the settlement contract enforces before paying the merchant.
  const leg = sdk.flow(chainId, {
    name: `${name}:swap`,
    inputs: { funding: resources.erc20(payToken, chainId) },
  });
  leg.lifi.swap('swap', {
    bind: { amountIn: leg.inputs.funding },
    config: {
      resourceOut: resources.erc20(merchantToken, chainId),
      slippage: inputs.slippageBps / 10_000,
    },
  });

  const flow = sdk.flow(chainId, {
    name,
    inputs: { payment: resources.erc20(payToken, chainId) },
  });
  flow.continuation.settle('settle', {
    bind: { input: flow.inputs.payment },
    config: {
      provider: 'intent-factory',
      // The escrow transfer is a separate action any wallet can send.
      funding: 'external',
      outcomes: [{ token: merchantToken, destination: merchant, chainId }],
      validity: { expiresAtMs: Date.now() + 60 * 60 * 1000 },
      continuationFlow: leg.build(),
    },
  });

  // External funding takes a plain amount: the customer, not the signer, pays.
  const run = {
    signer: merchant,
    inputs: { payment: inputs.amount },
  } satisfies ComposeRunInput;
  return { flow, run };
};
```

### Call it

```ts
import { createComposeSdk } from '@lifi/composer-sdk';

import { buildFlow } from './qr-checkout';

const sdk = createComposeSdk({
  baseUrl: 'https://composer.li.quest',
  apiKey: process.env.LIFI_API_KEY!,
});

// inputs: the values your user picks; see the module's inputs interface.
const { flow, run } = buildFlow(sdk, inputs);
const result = await flow.compile({ ...run, simulationPolicy: 'allow-revert' });
// Show the outputs' amount.minimum to the user, then have them sign
// result.transaction.
```

### The same call over REST

Compile and simulate in one call, for sample values:

```http
POST https://composer.li.quest/compose
content-type: application/json
x-lifi-api-key: <your key>

{
  "flow": {
    "version": 1,
    "id": "tpl:qr-checkout@1",
    "chainId": 8453,
    "inputs": [
      {
        "name": "payment",
        "resource": {
          "kind": "erc20",
          "token": "0x4200000000000000000000000000000000000006",
          "chainId": 8453
        }
      }
    ],
    "nodes": [
      {
        "id": "settle",
        "op": "continuation.settle",
        "bind": {
          "input": {
            "$ref": "input.payment"
          }
        },
        "config": {
          "provider": "intent-factory",
          "funding": "external",
          "outcomes": [
            {
              "token": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
              "destination": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
              "chainId": 8453
            }
          ],
          "validity": {
            "expiresAtMs": 1794933828259
          },
          "continuationFlow": {
            "version": 1,
            "id": "tpl:qr-checkout@1:swap",
            "chainId": 8453,
            "inputs": [
              {
                "name": "funding",
                "resource": {
                  "kind": "erc20",
                  "token": "0x4200000000000000000000000000000000000006",
                  "chainId": 8453
                }
              }
            ],
            "nodes": [
              {
                "id": "swap",
                "op": "lifi.swap",
                "bind": {
                  "amountIn": {
                    "$ref": "input.funding"
                  }
                },
                "config": {
                  "resourceOut": {
                    "kind": "erc20",
                    "token": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
                    "chainId": 8453
                  },
                  "slippage": 0.005
                }
              }
            ]
          }
        }
      }
    ]
  },
  "run": {
    "inputs": {
      "payment": "10000000000000000"
    },
    "signer": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"
  }
}
```

### Prompt for a coding agent

```text
Goal: add "Get paid by QR code" to my app with LI.FI Composer.
1. Install exactly: npm install @lifi/composer-sdk@0.16.0 @lifi/compose-spec@0.16.0
2. Read https://composer.li.quest/llms.txt. The manifest at https://composer.li.quest/compose/manifest is the source of truth for operations.
3. The flow: Customer scans and pays → Lands in escrow (continuation.settle) → Delivered as your token (lifi.swap).
   What can change: Token you receive, Where it lands.
4. Name the flow "tpl:qr-checkout@1" so LI.FI can see which template it came from.
5. Compile with POST /compose and simulationPolicy: 'allow-revert'.
   Before the user signs, show outputs[<key>].amount.minimum.value: the least they receive.
   It is optional; when it is absent, the flow has no floor.
6. The API key comes from https://portal.li.fi. Keep it on the server.
7. If you get stuck, POST https://composer.li.quest/feedback with what you tried.
```

## Composer references

- [Composer API llms.txt](https://composer.li.quest/llms.txt)
- [Operation manifest](https://composer.li.quest/compose/manifest): the source of truth for operations
- [Docs](https://docs.li.fi/composer/overview.md)
- Stuck? `POST https://composer.li.quest/feedback` with what you tried; it needs no key.
