|
| 1 | +--- |
| 2 | +title: "Nuxt integration" |
| 3 | +description: "Add Wraith to your Nuxt 3 app with auto-imported composables and SSR-safe patterns" |
| 4 | +--- |
| 5 | + |
| 6 | +Use the official Nuxt module to add Wraith to your Nuxt 3 application. The module auto-imports composables, handles SSR safety, and lets you use Wraith with native Nuxt patterns. |
| 7 | + |
| 8 | +## Install the module |
| 9 | + |
| 10 | +```bash |
| 11 | +npx nuxi module add @wraith-protocol/nuxt |
| 12 | +``` |
| 13 | + |
| 14 | +This registers the module in your `nuxt.config.ts`: |
| 15 | + |
| 16 | +```ts |
| 17 | +export default defineNuxtConfig({ |
| 18 | + modules: ["@wraith-protocol/nuxt"], |
| 19 | + wraith: { |
| 20 | + apiKey: process.env.NUXT_PUBLIC_WRAITH_API_KEY, |
| 21 | + }, |
| 22 | +}); |
| 23 | +``` |
| 24 | + |
| 25 | +Set your API key in `.env`: |
| 26 | + |
| 27 | +```bash |
| 28 | +NUXT_PUBLIC_WRAITH_API_KEY=wraith_live_abc123 |
| 29 | +``` |
| 30 | + |
| 31 | +## Auto-imported composables |
| 32 | + |
| 33 | +The module auto-imports three composables. No manual imports needed. |
| 34 | + |
| 35 | +### `useWraith()` |
| 36 | + |
| 37 | +Returns a shared `Wraith` client configured with the API key from `nuxt.config.ts`. |
| 38 | + |
| 39 | +```ts |
| 40 | +import { Chain } from "@wraith-protocol/sdk"; |
| 41 | + |
| 42 | +const wraith = useWraith(); |
| 43 | + |
| 44 | +const agent = await wraith.createAgent({ |
| 45 | + name: "alice", |
| 46 | + chain: Chain.Horizen, |
| 47 | + wallet: walletAddress, |
| 48 | + signature: signature, |
| 49 | +}); |
| 50 | +``` |
| 51 | + |
| 52 | +### `useWraithAgent()` |
| 53 | + |
| 54 | +Returns the currently active agent, or `null` if no agent has been created yet. |
| 55 | + |
| 56 | +```ts |
| 57 | +const agent = useWraithAgent(); |
| 58 | + |
| 59 | +if (agent) { |
| 60 | + const res = await agent.chat("send 0.1 ETH to bob.wraith"); |
| 61 | + console.log(res.response); |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +### `useWraithBalance()` |
| 66 | + |
| 67 | +Reactive balance state for the active agent. Refreshes on an interval. |
| 68 | + |
| 69 | +```ts |
| 70 | +const { balance, refresh } = useWraithBalance(); |
| 71 | + |
| 72 | +// balance.value is a Ref<{ native: string; tokens: Record<string, string> }> |
| 73 | +console.log(balance.value.native); // "1.5" |
| 74 | +console.log(balance.value.tokens); // { ZEN: "100.0", USDC: "50.0" } |
| 75 | +``` |
| 76 | + |
| 77 | +## SSR gotchas |
| 78 | + |
| 79 | +The Wraith SDK uses `fetch` and has no Node.js-specific dependencies. The managed agent client (`useWraith`, `useWraithAgent`) is safe to use during SSR. Crypto modules require client-side only. |
| 80 | + |
| 81 | +### Crypto modules are client-only |
| 82 | + |
| 83 | +If you import chain-specific crypto modules (`@wraith-protocol/sdk/chains/evm`, etc.), they must run client-side. These modules use `@noble/curves` which works in the browser but causes hydration mismatches during SSR. |
| 84 | + |
| 85 | +Use dynamic imports guarded by `import.meta.client`: |
| 86 | + |
| 87 | +```ts |
| 88 | +let generateStealthMetaAddress: typeof import("@wraith-protocol/sdk/chains/evm").generateStealthMetaAddress; |
| 89 | + |
| 90 | +if (import.meta.client) { |
| 91 | + const evm = await import("@wraith-protocol/sdk/chains/evm"); |
| 92 | + generateStealthMetaAddress = evm.generateStealthMetaAddress; |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +### Client-only components |
| 97 | + |
| 98 | +For UI components that interact with Wraith, mark them with the `.client.vue` suffix or use `<ClientOnly>`: |
| 99 | + |
| 100 | +```vue |
| 101 | +<template> |
| 102 | + <ClientOnly> |
| 103 | + <WraithDashboard /> |
| 104 | + <template #fallback> |
| 105 | + <div class="skeleton">Loading Wraith dashboard...</div> |
| 106 | + </template> |
| 107 | + </ClientOnly> |
| 108 | +</template> |
| 109 | +``` |
| 110 | + |
| 111 | +### Module SSR configuration |
| 112 | + |
| 113 | +The module registers a Nuxt plugin that initializes the `Wraith` client. It defaults to `ssr: false` (client-only). Toggle this in `nuxt.config.ts`: |
| 114 | + |
| 115 | +```ts |
| 116 | +export default defineNuxtConfig({ |
| 117 | + wraith: { |
| 118 | + apiKey: process.env.NUXT_PUBLIC_WRAITH_API_KEY, |
| 119 | + ssr: false, // default — runs Wraith client in browser only |
| 120 | + }, |
| 121 | +}); |
| 122 | +``` |
| 123 | + |
| 124 | +Set `ssr: true` to initialize the Wraith client on the server too. The composables will work during SSR, but crypto modules remain client-only regardless. |
| 125 | + |
| 126 | +## Full send and receive example |
| 127 | + |
| 128 | +This composable handles agent creation, sending stealth payments, and scanning for incoming payments: |
| 129 | + |
| 130 | +```ts no-check |
| 131 | +import { Chain } from "@wraith-protocol/sdk"; |
| 132 | + |
| 133 | +export function useWraithPayment() { |
| 134 | + const wraith = useWraith(); |
| 135 | + const agentState = useWraithAgent(); |
| 136 | + const { balance, refresh: refreshBalance } = useWraithBalance(); |
| 137 | + |
| 138 | + const sending = ref(false); |
| 139 | + const error = ref(""); |
| 140 | + const lastResponse = ref(""); |
| 141 | + |
| 142 | + const ensureAgent = async () => { |
| 143 | + if (agentState.value) return; |
| 144 | + |
| 145 | + const newAgent = await wraith.createAgent({ |
| 146 | + name: "alice", |
| 147 | + chain: Chain.Horizen, |
| 148 | + wallet: walletAddress, |
| 149 | + signature: signature, |
| 150 | + }); |
| 151 | + agentState.value = newAgent; |
| 152 | + }; |
| 153 | + |
| 154 | + const send = async (to: string, amount: string, asset: string) => { |
| 155 | + sending.value = true; |
| 156 | + error.value = ""; |
| 157 | + try { |
| 158 | + const res = await agentState.value!.chat( |
| 159 | + `send ${amount} ${asset} to ${to}` |
| 160 | + ); |
| 161 | + lastResponse.value = res.response; |
| 162 | + await refreshBalance(); |
| 163 | + return res; |
| 164 | + } catch (err) { |
| 165 | + error.value = (err as Error).message; |
| 166 | + throw err; |
| 167 | + } finally { |
| 168 | + sending.value = false; |
| 169 | + } |
| 170 | + }; |
| 171 | + |
| 172 | + const scan = async () => { |
| 173 | + const res = await agentState.value!.chat("scan for incoming payments"); |
| 174 | + lastResponse.value = res.response; |
| 175 | + return res; |
| 176 | + }; |
| 177 | + |
| 178 | + return { |
| 179 | + agent: agentState, |
| 180 | + balance, |
| 181 | + sending, |
| 182 | + error, |
| 183 | + lastResponse, |
| 184 | + ensureAgent, |
| 185 | + send, |
| 186 | + scan, |
| 187 | + }; |
| 188 | +} |
| 189 | +``` |
| 190 | + |
| 191 | +Use it in a component: |
| 192 | + |
| 193 | +```vue |
| 194 | +<script setup lang="ts"> |
| 195 | +const { |
| 196 | + balance, |
| 197 | + lastResponse, |
| 198 | + error, |
| 199 | + sending, |
| 200 | + ensureAgent, |
| 201 | + send, |
| 202 | + scan, |
| 203 | +} = useWraithPayment(); |
| 204 | +
|
| 205 | +const recipient = ref("bob.wraith"); |
| 206 | +const amount = ref("0.1"); |
| 207 | +const asset = ref("ETH"); |
| 208 | +
|
| 209 | +onMounted(() => ensureAgent()); |
| 210 | +</script> |
| 211 | +
|
| 212 | +<template> |
| 213 | + <ClientOnly> |
| 214 | + <div> |
| 215 | + <div v-if="error" class="error">{{ error }}</div> |
| 216 | +
|
| 217 | + <section> |
| 218 | + <h2>Balance</h2> |
| 219 | + <p>{{ balance?.native ?? "—" }} {{ asset }}</p> |
| 220 | + </section> |
| 221 | +
|
| 222 | + <section> |
| 223 | + <h2>Send payment</h2> |
| 224 | + <input v-model="recipient" placeholder="bob.wraith" /> |
| 225 | + <input v-model="amount" type="number" placeholder="0.1" /> |
| 226 | + <select v-model="asset"> |
| 227 | + <option>ETH</option> |
| 228 | + <option>USDC</option> |
| 229 | + <option>ZEN</option> |
| 230 | + </select> |
| 231 | + <button :disabled="sending" @click="send(recipient, amount, asset)"> |
| 232 | + {{ sending ? "Sending..." : "Send" }} |
| 233 | + </button> |
| 234 | + <p v-if="lastResponse">{{ lastResponse }}</p> |
| 235 | + </section> |
| 236 | +
|
| 237 | + <section> |
| 238 | + <h2>Incoming payments</h2> |
| 239 | + <button @click="scan">Scan</button> |
| 240 | + </section> |
| 241 | + </div> |
| 242 | + <template #fallback> |
| 243 | + <p>Loading Wraith...</p> |
| 244 | + </template> |
| 245 | + </ClientOnly> |
| 246 | +</template> |
| 247 | +``` |
| 248 | + |
| 249 | +Payments sent via `send` go to a fresh stealth address. The recipient can detect them by calling `scan`. Both operations happen through the AI agent running in the TEE — you never touch chain-specific crypto directly. |
| 250 | + |
| 251 | +## Configuration reference |
| 252 | + |
| 253 | +Full `wraith` options in `nuxt.config.ts`: |
| 254 | + |
| 255 | +| Option | Type | Default | Description | |
| 256 | +|---|---|---|---| |
| 257 | +| `apiKey` | `string` | Required | Your Wraith API key | |
| 258 | +| `ssr` | `boolean` | `false` | Enable server-side initialization of the Wraith client | |
| 259 | +| `baseUrl` | `string` | `"https://api.wraith.dev"` | API base URL override | |
| 260 | +| `ai.provider` | `"gemini"` \| `"openai"` \| `"claude"` | `"gemini"` | AI provider for agent chat | |
| 261 | +| `ai.apiKey` | `string` | `undefined` | Your AI provider API key | |
| 262 | + |
| 263 | +## Type safety |
| 264 | + |
| 265 | +Types ship with the module. Add them to your `tsconfig.json`: |
| 266 | + |
| 267 | +```json |
| 268 | +{ |
| 269 | + "compilerOptions": { |
| 270 | + "types": ["@wraith-protocol/nuxt"] |
| 271 | + } |
| 272 | +} |
| 273 | +``` |
| 274 | + |
| 275 | +All composable return types are inferred from `@wraith-protocol/sdk`. `useWraith()` returns `Wraith`, `useWraithAgent()` returns `WraithAgent | null`, and `useWraithBalance()` returns a typed reactive balance object. |
| 276 | + |
| 277 | +## Next steps |
| 278 | + |
| 279 | +- [Single-chain agent guide](/guides/single-chain-agent) — full agent lifecycle |
| 280 | +- [Multichain agent](/guides/multichain-agent) — deploy across multiple chains |
| 281 | +- [SDK overview](/sdk/overview) — all SDK entry points |
| 282 | +- [Bring your own model](/guides/bring-your-own-model) — use OpenAI or Claude |
0 commit comments