|
1 | | -# x402-openai-typescript |
2 | | -Drop-in OpenAI Typescript client with transparent x402 payment support. |
| 1 | +# x402-openai |
| 2 | + |
| 3 | +> Drop-in OpenAI TypeScript client with transparent x402 payment support — **EVM, Solana, and beyond**. |
| 4 | +
|
| 5 | +[](https://typescriptlang.org) |
| 6 | +[](LICENSE) |
| 7 | + |
| 8 | +## Install |
| 9 | + |
| 10 | +```bash |
| 11 | +# EVM only |
| 12 | +bun add x402-openai openai @x402/fetch @x402/evm viem |
| 13 | + |
| 14 | +# Solana (SVM) only |
| 15 | +bun add x402-openai openai @x402/fetch @x402/svm @solana/kit @scure/base |
| 16 | + |
| 17 | +# Both chains |
| 18 | +bun add x402-openai openai @x402/fetch @x402/evm @x402/svm viem @solana/kit @scure/base |
| 19 | +``` |
| 20 | + |
| 21 | +## Quick Start |
| 22 | + |
| 23 | +### EVM (Ethereum / Base / …) |
| 24 | + |
| 25 | +```ts |
| 26 | +import { X402OpenAI } from "x402-openai"; |
| 27 | +import { EvmWallet } from "x402-openai/wallets"; |
| 28 | + |
| 29 | +const client = new X402OpenAI({ |
| 30 | + wallet: new EvmWallet({ privateKey: "0x…" }), |
| 31 | +}); |
| 32 | + |
| 33 | +// Use exactly like openai.OpenAI — everything just works! |
| 34 | +const completion = await client.chat.completions.create({ |
| 35 | + model: "gpt-4o-mini", |
| 36 | + messages: [{ role: "user", content: "Hello!" }], |
| 37 | +}); |
| 38 | +console.log(completion.choices[0]?.message.content); |
| 39 | +``` |
| 40 | + |
| 41 | +### SVM (Solana) |
| 42 | + |
| 43 | +```ts |
| 44 | +import { X402OpenAI } from "x402-openai"; |
| 45 | +import { SvmWallet } from "x402-openai/wallets"; |
| 46 | + |
| 47 | +const client = new X402OpenAI({ |
| 48 | + wallet: new SvmWallet({ privateKey: "base58…" }), |
| 49 | +}); |
| 50 | +``` |
| 51 | + |
| 52 | +### Multi-chain |
| 53 | + |
| 54 | +Register multiple wallets — the x402 protocol automatically selects the right chain based on the server's payment requirements. |
| 55 | + |
| 56 | +```ts |
| 57 | +import { X402OpenAI } from "x402-openai"; |
| 58 | +import { EvmWallet, SvmWallet } from "x402-openai/wallets"; |
| 59 | + |
| 60 | +const client = new X402OpenAI({ |
| 61 | + wallets: [ |
| 62 | + new EvmWallet({ privateKey: "0x…" }), |
| 63 | + new SvmWallet({ privateKey: "base58…" }), |
| 64 | + ], |
| 65 | +}); |
| 66 | +``` |
| 67 | + |
| 68 | +### EVM mnemonic phrase |
| 69 | + |
| 70 | +```ts |
| 71 | +import { EvmWallet } from "x402-openai/wallets"; |
| 72 | + |
| 73 | +const wallet = new EvmWallet({ mnemonic: "word1 word2 … word12" }); |
| 74 | + |
| 75 | +// BIP-44 account #2 |
| 76 | +const wallet2 = new EvmWallet({ mnemonic: "word1 word2 …", accountIndex: 2 }); |
| 77 | + |
| 78 | +// Custom derivation path |
| 79 | +const wallet3 = new EvmWallet({ |
| 80 | + mnemonic: "word1 word2 …", |
| 81 | + derivationPath: "m/44'/60'/2'/0/0", |
| 82 | +}); |
| 83 | +``` |
| 84 | + |
| 85 | +### Streaming |
| 86 | + |
| 87 | +```ts |
| 88 | +const stream = await client.chat.completions.create({ |
| 89 | + model: "gpt-4o-mini", |
| 90 | + messages: [{ role: "user", content: "Explain x402" }], |
| 91 | + stream: true, |
| 92 | +}); |
| 93 | + |
| 94 | +for await (const chunk of stream) { |
| 95 | + const content = chunk.choices[0]?.delta?.content; |
| 96 | + if (content) process.stdout.write(content); |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +### Advanced: pre-built x402 client |
| 101 | + |
| 102 | +```ts |
| 103 | +import { x402Client, wrapFetchWithPayment } from "@x402/fetch"; |
| 104 | +import { X402OpenAI } from "x402-openai"; |
| 105 | + |
| 106 | +const x402 = new x402Client(); |
| 107 | +// ... register custom payment schemes ... |
| 108 | + |
| 109 | +const client = new X402OpenAI({ x402Client: x402 }); |
| 110 | +``` |
| 111 | + |
| 112 | +## API |
| 113 | + |
| 114 | +### `new X402OpenAI(options)` |
| 115 | + |
| 116 | +Drop-in replacement for `openai.OpenAI`. Provide **exactly one** credential source: |
| 117 | + |
| 118 | +| Parameter | Description | |
| 119 | +| ------------ | --------------------------------------------------- | |
| 120 | +| `wallet` | A single `Wallet` adapter (e.g. `EvmWallet`) | |
| 121 | +| `wallets` | List of `Wallet` adapters for multi-chain support | |
| 122 | +| `x402Client` | Pre-configured `x402Client` | |
| 123 | + |
| 124 | +Default `baseURL` is `https://llm.qntx.fun/v1`. All standard OpenAI constructor options (`baseURL`, `timeout`, `maxRetries`, …) are forwarded transparently. |
| 125 | + |
| 126 | +### Wallet adapters |
| 127 | + |
| 128 | +| Class | Chain | Install extras | |
| 129 | +| -------------------------------- | ------------------------ | ----------------------------------------- | |
| 130 | +| `EvmWallet({ privateKey: … })` | EVM (Ethereum, Base, …) | `@x402/evm viem` | |
| 131 | +| `EvmWallet({ mnemonic: … })` | EVM (BIP-39) | `@x402/evm viem` | |
| 132 | +| `SvmWallet({ privateKey: … })` | Solana | `@x402/svm @solana/kit @scure/base` | |
| 133 | + |
| 134 | +All wallets implement the `Wallet` interface. See [`src/wallets/base.ts`](src/wallets/base.ts) to add support for a new chain. |
| 135 | + |
| 136 | +## Examples |
| 137 | + |
| 138 | +```bash |
| 139 | +# EVM — private key |
| 140 | +EVM_PRIVATE_KEY="0x..." bun examples/chat-evm.ts |
| 141 | + |
| 142 | +# EVM — mnemonic phrase |
| 143 | +MNEMONIC="word1 word2 ..." bun examples/chat-evm-mnemonic.ts |
| 144 | + |
| 145 | +# SVM (Solana) — private key |
| 146 | +SOLANA_PRIVATE_KEY="base58..." bun examples/chat-svm.ts |
| 147 | + |
| 148 | +# Streaming — EVM |
| 149 | +EVM_PRIVATE_KEY="0x..." bun examples/streaming-evm.ts |
| 150 | + |
| 151 | +# Streaming — EVM mnemonic |
| 152 | +MNEMONIC="word1 word2 ..." bun examples/streaming-evm-mnemonic.ts |
| 153 | + |
| 154 | +# Streaming — SVM |
| 155 | +SOLANA_PRIVATE_KEY="base58..." bun examples/streaming-svm.ts |
| 156 | + |
| 157 | +# Multi-chain (EVM + SVM) |
| 158 | +EVM_PRIVATE_KEY="0x..." SOLANA_PRIVATE_KEY="base58..." bun examples/multichain.ts |
| 159 | + |
| 160 | +# List models |
| 161 | +EVM_PRIVATE_KEY="0x..." bun examples/models.ts |
| 162 | + |
| 163 | +# List models — mnemonic |
| 164 | +MNEMONIC="word1 word2 ..." bun examples/models-mnemonic.ts |
| 165 | +``` |
| 166 | + |
| 167 | +## Development |
| 168 | + |
| 169 | +```bash |
| 170 | +# Install dependencies |
| 171 | +bun install |
| 172 | + |
| 173 | +# Run tests |
| 174 | +bun test |
| 175 | + |
| 176 | +# Type check |
| 177 | +bun run typecheck |
| 178 | +``` |
| 179 | + |
| 180 | +## License |
| 181 | + |
| 182 | +This project is licensed under the [MIT License](LICENSE). |
0 commit comments