> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mobula.io/llms.txt
> Use this file to discover all available pages before exploring further.

# MPP Create API key

> GET /agent/mpp/api-keys/create — Pay a small fixed fee; returns a new api_key for the agent wallet.

<Note>
  **Beta.** Same as [x402 create API key](/guides/x402-agent-api-key-create) except path and MPP settlement. Overview: [Agentic payments](/guides/agentic-payments).
</Note>

## Endpoint

```
GET https://mpp.mobula.io/agent/mpp/api-keys/create
```

No query parameters. Payer must match the subscription wallet.

## Payment

Fixed fee in USD (see [`openapi.json`](https://api.mobula.io/openapi.json)). **402** → mppx pay → retry → **200** with `api_key` and `user_id`.

***

## Reference script

Same pattern as [x402 Create API key](/guides/x402-agent-api-key-create): full runnable example, but for **MPP** / **`mppx`** on Tempo.

**Prerequisites:** `MPPX_PRIVATE_KEY` (`0x...`), [`mppx`](https://docs.tempo.xyz/mpp) (`bunx mppx`), **bun**, **viem**. Run from the **monorepo root**.

**Source:** `agent-api-key-create.ts`

```typescript theme={null}
/**
 * MPP agent API key create — pay fixed fee; returns a new api_key for this agent wallet.
 *
 * Usage:
 *   MPPX_PRIVATE_KEY=0x<key> bun run scripts/src/mpp/agent-api-key-create.ts
 *
 * Optional: API_URL (default: https://api.mobula.io)
 */

import { createPublicClient, formatUnits, http } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';

const API_URL = process.env.API_URL ?? 'https://mpp.mobula.io';

const PATHUSD = '0x20c0000000000000000000000000000000000000' as const;
const PATHUSD_DECIMALS = 6;
const PRIVATE_KEY = process.env.MPPX_PRIVATE_KEY as `0x${string}` | undefined;
if (!PRIVATE_KEY) {
  console.error('ERROR: Set MPPX_PRIVATE_KEY=0x<your_private_key>');
  process.exit(1);
}

const payer = privateKeyToAccount(PRIVATE_KEY as `0x${string}`);

const client = createPublicClient({
  chain: {
    id: 42431,
    name: 'Tempo Testnet',
    nativeCurrency: { name: 'ETH', symbol: 'ETH', decimals: 18 },
    rpcUrls: { default: { http: ['https://rpc.moderato.tempo.xyz'] } },
  },
  transport: http(),
});

const balanceOfAbi = [
  {
    type: 'function',
    name: 'balanceOf',
    inputs: [{ name: 'account', type: 'address' }],
    outputs: [{ name: '', type: 'uint256' }],
    stateMutability: 'view',
  },
] as const;

async function getPathUsdBalance(address: `0x${string}`): Promise<bigint> {
  return client.readContract({
    address: PATHUSD,
    abi: balanceOfAbi,
    functionName: 'balanceOf',
    args: [address],
  });
}

function fmtBalance(raw: bigint): string {
  return `${formatUnits(raw, PATHUSD_DECIMALS)} PathUSD`;
}

async function getRecipientFromChallenge(url: string): Promise<`0x${string}`> {
  const res = await fetch(url);
  const wwwAuth = res.headers.get('www-authenticate') || '';
  const requestMatch = wwwAuth.match(/request="([^"]*)"/);
  if (requestMatch) {
    const decoded = JSON.parse(Buffer.from(requestMatch[1], 'base64').toString());
    return decoded.recipient;
  }
  throw new Error('Could not parse recipient from challenge');
}

function mppxRequest(endpoint: string): string {
  const { execSync } = require('child_process');
  const result = execSync(`MPPX_PRIVATE_KEY=${PRIVATE_KEY} bunx mppx "${endpoint}" -v`, {
    encoding: 'utf-8',
    cwd: process.cwd(),
    timeout: 60_000,
  });
  return result;
}

const endpointUrl = `${API_URL}/agent/mpp/api-keys/create`;
const recipient = await getRecipientFromChallenge(endpointUrl);

console.log('\n--- Addresses ---');
console.log(`  Payer:     ${payer.address}`);
console.log(`  Recipient: ${recipient}`);

console.log('\n--- Step 1: Balances BEFORE payment ---\n');
const payerBefore = await getPathUsdBalance(payer.address);
const recipientBefore = await getPathUsdBalance(recipient);
console.log(`  Payer:     ${fmtBalance(payerBefore)}`);
console.log(`  Recipient: ${fmtBalance(recipientBefore)}`);

console.log('\n--- Step 2: Create API key — $0.001 ---\n');
try {
  const result = mppxRequest(endpointUrl);
  console.log(result);
} catch (err: unknown) {
  const execErr = err as { stdout?: string; stderr?: string };
  console.error('mppx CLI failed:');
  if (execErr.stdout) console.log(execErr.stdout);
  if (execErr.stderr) console.error(execErr.stderr);
  process.exit(1);
}

console.log('--- Step 3: Balances AFTER payment ---\n');
const payerAfter = await getPathUsdBalance(payer.address);
const recipientAfter = await getPathUsdBalance(recipient);
console.log(`  Payer:     ${fmtBalance(payerAfter)}`);
console.log(`  Recipient: ${fmtBalance(recipientAfter)}`);

console.log('\n--- Balance changes ---\n');
console.log(
  `  Payer:     ${fmtBalance(payerBefore)} → ${fmtBalance(payerAfter)} (${fmtBalance(payerAfter - payerBefore)})`,
);
console.log(
  `  Recipient: ${fmtBalance(recipientBefore)} → ${fmtBalance(recipientAfter)} (+${fmtBalance(recipientAfter - recipientBefore)})`,
);

process.exit(0);
```

```bash theme={null}
MPPX_PRIVATE_KEY=0x<key> bun run agent-api-key-create.ts
```

## See also

* [x402 Create API key](/guides/x402-agent-api-key-create)
* [MPP Revoke API key](/guides/mpp-agent-api-key-revoke)
