Skip to main content
Obtain your AUTH_TOKEN and ENVIRONMENT_ID from the Dynamic dashboard and enable embedded wallets for the chains you plan to target.

1. Install dependencies

npm install @dynamic-labs/node x402-fetch dotenv

2. Configure the Dynamic SDK

import { DynamicServerWallets } from '@dynamic-labs/node';
import { wrapFetchWithPayment } from 'x402-fetch';
import dotenv from 'dotenv';

dotenv.config();

const dynamic = new DynamicServerWallets({
  environmentId: process.env.DYNAMIC_ENVIRONMENT_ID!,
  authToken: process.env.DYNAMIC_AUTH_TOKEN!,
});

3. Create an MPC wallet

const wallet = await dynamic.createWallet({
  chain: 'EVM',
  chainId: 84532, // Base Sepolia for staging
  label: 'horizon-x402-wallet',
});

console.log('Wallet address', wallet.address);
Fund the wallet so it can pay invoices for x402-protected endpoints.

4. Wrap fetch with a Dynamic signer

const fetchWithPayment = wrapFetchWithPayment(fetch, {
  async signMessage(message: string) {
    const { signature } = await dynamic.signMessage({
      walletId: wallet.id,
      message,
    });
    return signature;
  },
});

5. Invoke Horizon endpoints

const baseUrl = process.env.HORIZON_BASE_URL ?? 'https://api.horizon.new/v1';

const response = await fetchWithPayment(`${baseUrl}/extract/pdf`, {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    sourceUrl: 'https://cdn.example.com/playbooks/launch.pdf',
    sourceName: 'Launch Playbook',
    metadata: { team: 'GoToMarket' },
  }),
});

const job = await response.json();
console.log('Job ID', job.jobId);

6. Monitor job progress

const status = await fetchWithPayment(job.statusUrl).then((res) => res.json());

if (status.state === 'failed') {
  console.error(status.error);
}

Notes

  • Switch chainId to your production network when you are ready to ship.
  • Dynamic wallets support threshold signatures—use higher quorum levels for sensitive transactions and approve them via Dynamic’s policy engine.