Nuxt SDK
Official Nuxt module for CaptchaLa CAPTCHA — published as @captcha-la/nuxt. Current version: 1.0.1.
- Auto-registers a client-only
<Captchala>component (built on@captcha-la/vue) - Server utils
validateCaptchalaToken()andissueCaptchalaServerToken(), auto-imported in your server routes - The app secret stays in the private runtime config and is never sent to the browser
- Loads the web SDK from our CDN at runtime
Install
npm install @captcha-la/nuxt
# or
yarn add @captcha-la/nuxt
# or
pnpm add @captcha-la/nuxt// nuxt.config.ts
export default defineNuxtConfig({
modules: ['@captcha-la/nuxt'],
captchala: {
appKey: 'your-app-key' // public; or NUXT_PUBLIC_CAPTCHALA_APP_KEY
}
})# .env (server only)
NUXT_CAPTCHALA_APP_SECRET=your-app-secretQuick start
<template>
<Captchala
product="popup"
action="login"
@success="handleSuccess"
@error="handleError"
/>
</template>
<script setup>
function handleSuccess(result) {
// Send result.token to your server for validation.
console.log('pass_token:', result.token)
}
function handleError(error) {
console.error('verification failed:', error)
}
</script>No import is needed. app-key defaults to the module's appKey; pass it to override. The component is client-only: on the server it renders an empty placeholder, and the SDK loads in the browser.
Module options
| Option | Type | Default | Description |
|---|---|---|---|
appKey | string | '' | Public app key: default app-key of the component, and the key for the server utils. Runtime override: NUXT_PUBLIC_CAPTCHALA_APP_KEY (component) / NUXT_CAPTCHALA_APP_KEY (server utils) |
appSecret | string | '' | App secret for the server utils (private runtime config). Prefer NUXT_CAPTCHALA_APP_SECRET |
componentName | string | 'Captchala' | Name of the auto-registered component |
serverUtils | boolean | true | Register the server utils |
Props
| Prop | Type | Default | Description |
|---|---|---|---|
appKey | string | module appKey | CaptchaLa application key |
serverToken | string | - | One-time, server-issued challenge token. Required when the app has server_token_required=true. See serverToken mode |
product | 'popup' | 'float' | 'embed' | 'bind' | 'popup' | Display mode |
action | string | 'default' | Action identifier (e.g. login, register, checkout) |
lang | string | browser language | Language code (zh-CN, en, ja, …); unset = the browser language |
theme | string | object | follows the system | Preset string or { custom: { brightness: 'light' | 'dark' | 'system', ... } } (see Web SDK options) |
bindTo | string | HTMLElement | - | Element or selector to bind to (only for product="bind") |
onServerTokenExpired | () => Promise<string | null> | string | null | - | Called when serverToken is rejected; return a new token to retry once, or null |
Events
| Event | Payload | Description |
|---|---|---|
success | { token, type, action } | Verification succeeded |
error | error | Verification failed |
close | - | CAPTCHA closed |
expired | - | The pass token expired before it was used (once); with a listener, emitted instead of error; the widget shows the expiry. See Token expiry |
ready | - | CAPTCHA ready |
Methods (via ref)
<template>
<Captchala ref="captchaRef" />
<button @click="captchaRef?.verify()">Verify</button>
</template>
<script setup>
const captchaRef = ref()
</script>| Method | Description |
|---|---|
verify() | Trigger verification |
reset() | Reset CAPTCHA state |
destroy() | Destroy the instance |
bindTo(selector) | Bind to element (for bind mode) |
setLang(lang) | Switch language in place |
getInstance() | The underlying web SDK instance |
The ref is set once the component has mounted in the browser.
Display modes
<Captchala product="popup" /> <!-- button that opens a popup (default) -->
<Captchala product="float" /> <!-- inline floating widget -->
<Captchala product="embed" /> <!-- embedded in the page -->
<Captchala product="bind" bind-to="#submit-button" /> <!-- on your own button -->Server-side validation
validateCaptchalaToken(event, passToken, options?) is auto-imported in server/. It calls POST /v1/validate with pass_token and client_ip, using the key and secret from the private runtime config:
// server/api/login.post.ts
export default defineEventHandler(async (event) => {
const { email, password, captchaToken } = await readBody(event)
// captchaToken = result.token from @success
const captcha = await validateCaptchalaToken(event, captchaToken)
if (!captcha.valid) {
throw createError({ statusCode: 400, statusMessage: 'CAPTCHA verification failed' })
}
// Token is valid, proceed with the request
})Result: { valid, code, message?, data? }. valid is true only when the API returned code: 0 and data.valid: true; a missing token, network error or refusal gives valid: false (fail closed). Options:
| Option | Type | Default | Description |
|---|---|---|---|
clientIp | string | false | request IP | The end user's IP; false omits it |
xForwardedFor | boolean | false | Read the IP from X-Forwarded-For. Enable only behind a proxy that sets it |
For CI or local development you can use the public test keys as NUXT_CAPTCHALA_APP_KEY / NUXT_CAPTCHALA_APP_SECRET to exercise this path without solving a CAPTCHA.
Production: serverToken mode
For high-value flows (login, register, payment) we recommend the server-issued token flow. Your server mints a one-time server_token (5-minute TTL) with POST /v1/server/challenge/issue and the browser renders the widget with it. issueCaptchalaServerToken(event, { action, ttl }) does the call:
// server/api/captcha-token.get.ts
export default defineEventHandler(async (event) => ({
server_token: await issueCaptchalaServerToken(event, { action: 'login' })
}))<template>
<Captchala v-if="data" :server-token="data.server_token" action="login" @success="onSuccess" />
<div v-else>Loading…</div>
</template>
<script setup>
// Fetched in the browser on every mount: each token can be used once.
const { data } = await useFetch('/api/captcha-token', { server: false })
function onSuccess(result) {
console.log('pass_token:', result.token)
}
</script>- The module keeps the app secret in the private runtime config; never embed it in browser code.
- Each
server_tokencan be used exactly once. Fetch a new one before every widget mount. - Apps with
server_token_required=falsecan still use the publicappKey-only flow.
See API Reference for the full backend contract.
Token expiry
If the pass token runs out before the form uses it, the widget shows that the verification expired (in bind mode nothing is shown, since the button is yours) and @expired fires once, instead of @error. Start a new verification with reset() then verify() on the ref, or show your own prompt.
<template>
<Captchala
ref="captcha"
@success="(r) => { token = r.token; expired = false }"
@expired="() => { token = ''; expired = true }"
/>
<button v-if="expired" @click="verifyAgain">Verify again</button>
</template>
<script setup>
const captcha = ref()
const token = ref('')
const expired = ref(false)
function verifyAgain() {
expired.value = false
captcha.value?.reset()
captcha.value?.verify()
}
</script>Without an @expired listener, @error fires once (class expired). @expired never fires for offline or degraded tokens.
TypeScript
The module options are typed (captchala key in nuxt.config.ts), and the server utils are typed when auto-imported. Component types come from @captcha-la/vue:
import type { CaptchalaProps, CaptchalaResult } from '@captcha-la/vue'Versions
- 1.0.1 (2026-11-27): README in Japanese, Korean, Spanish, Portuguese (BR) and German; the server utils always call the CaptchaLa API endpoint.
- 1.0.0: first public release.