# AgentKit / plain x402 — Delx image workflow

The durable buyer quotes a Delx image, applies a price ceiling, and verifies
the delivered WebP. Payment requires the explicit `--pay` option. A separate
challenge-only example lets a Coinbase AgentKit or x402 client inspect HTTP 402.

## Why challenge-only?

Delx product calls require a real USDC payment on Base. This repo forbids
test spends without explicit owner approval. Validate discovery and the
challenge path first; pay only with a dedicated hot wallet after approval.

## Run

```bash
cd examples/agentkit-image
node challenge-only.mjs
```

Expected: process exit 0, JSON report with `"http_status": 402` and
`payment_required_header_present: true` (or a JSON body describing the
challenge).

## Safety

- Refuses to run if `X402_PRIVATE_KEY` or `PAYMENT_SIGNATURE` is set.
- Never logs full payment proofs when present.
- Uses `x-delx-source: example-agentkit-image` for demand attribution.

## Payment sequence

1. Call without payment → 402 + requirements.
2. Authorize the quoted USDC amount on the declared network.
3. Retry with `Payment-Signature` / facilitator-compatible headers.
4. Delx settles the payment before generating the image.
5. Verify `image_url`, `generation_id`, and SHA-256 on the receipt.

For a one-command AgentCash purchase (spends when a wallet is configured), see
`../agentcash-one-liners.md` — still owner-approved only.

## Durable image purchase

Follow the [public image quickstart](https://commerce.delx.ai/guides/verified-image-api)
to download the client and its standalone lockfile. From a Commerce checkout,
use the root workspace's `npm ci --ignore-scripts` instead. Both install the
pinned x402 SDK and WebP decoder independently of the server.

Quote first. This command sends an unpaid request and refuses an offer above
the ceiling. Keep each logical image purchase in its own private directory.

```bash
node examples/agentkit-image/image-workflow.mjs --directory /private/tmp/my-image-job --prompt "A friendly robot painting" --max-price 0.01
```

After the buyer authorizes spending, set `DELX_IMAGE_PRIVATE_KEY` through the
buyer's secret manager and add `--pay` to the same command. The client supports
Base USDC exact EIP-3009 authorizations. It signs one authorization, saves it
before transmission, and uses the server's `payment-identifier` extension.
It reuses the same payment proof for at most three attempts. A timeout, HTTP
409, or failed request keeps the saved authorization for a later resume with
the same directory, prompt and ceiling. Resuming never signs a replacement.
The wallet key is only needed until the first signature has been saved.

The output is `image.webp` plus a private `job.json`. The buyer verifies the
receipt price, HTTPS origin, MIME type, bounded byte length, SHA-256, WebP
container, dimensions and full pixel decoding before accepting the file.
A completed resume rechecks the local image and needs no network request.
Asset downloads never carry payment proofs. Proofs are removed from the state
as soon as a successful receipt is saved. Before that point, `job.json` contains
a spend authorization and must be protected like a credential. Keep all job
folders outside Git, backups shared with third parties and public output folders.

An exclusive `workflow.lock` prevents simultaneous runs in the same directory.
After an interrupted process, inspect the PID in that file and confirm the
process has exited before manually removing the lock and resuming the same job.
Keep the directory after any uncertain outcome. Creating a different directory
is a new purchase and can spend again. Expired or rejected authorizations require
operator investigation rather than automatic replacement.

If payment settled and the image still fails to arrive after resuming,
[email Delx about the payment](mailto:support@delx.ai?subject=Image%20payment%20review).
Include the public Base transaction hash if available. Keep your wallet key,
signed authorization and job files private.

All quote and paid requests use `x-delx-source: image-workflow-v1`. The Commerce
report attributes delivered external purchases and return buyers to this source
using verified payer fingerprints. Client tags alone do not establish identity.
The fixed source follows the entire request and replay path.

Run the full simulated buyer path without a wallet or paid upstream:

```bash
npm run image-workflow:test
```

The tests use an unfunded fixture key for local signatures and an in-process
HTTP fixture. They cover a response lost after settlement, interrupted resume,
concurrent invocation, rejected quotes and invalid image bytes. This evidence
covers local client behavior. Live settlement and generated-image quality need
separate authorized evidence. Distribution is handled by the Grok Bot.
