Skip to content

Angular SDK ​

Official Angular component for CaptchaLa CAPTCHA — published as @captcha-la/angular. Current version: 1.0.1.

  • Standalone component, no NgModule needed
  • injectCaptchala() for verifying on your own button (bind mode), with signals
  • Works with Angular 16 and later, zone.js or zoneless, and server-side rendering
  • Loads the web SDK from our CDN at runtime

Install ​

bash
npm install @captcha-la/angular
# or
yarn add @captcha-la/angular
# or
pnpm add @captcha-la/angular

Quick start ​

Component ​

ts
import { Component } from '@angular/core'
import { CaptchalaComponent, type CaptchalaResult } from '@captcha-la/angular'

@Component({
  selector: 'app-login',
  standalone: true,
  imports: [CaptchalaComponent],
  template: `
    <captchala
      appKey="your-app-key"
      product="popup"
      (success)="onSuccess($event)"
      (error)="onError($event)"
    />
  `
})
export class LoginComponent {
  onSuccess(result: CaptchalaResult) {
    // Send result.token to your server for validation.
    console.log('pass_token:', result.token)
  }

  onError(err: unknown) {
    console.error(err)
  }
}

With NgModules, add CaptchalaComponent to the module's imports.

injectCaptchala() ​

ts
import { Component } from '@angular/core'
import { injectCaptchala } from '@captcha-la/angular'

@Component({
  selector: 'app-login-form',
  standalone: true,
  template: `
    <form (submit)="onSubmit($event)">
      <input type="email" placeholder="Email" />
      <input type="password" placeholder="Password" />
      <button type="submit" [disabled]="!captcha.ready()">Login</button>
    </form>
  `
})
export class LoginFormComponent {
  // Destroyed automatically together with the component
  readonly captcha = injectCaptchala({
    appKey: 'your-app-key',
    product: 'bind',
    action: 'login'
  })

  async onSubmit(e: Event) {
    e.preventDefault()
    try {
      const result = await this.captcha.verify()
      // result.token → submit with the form
    } catch (err) {
      console.error('verification failed:', err)
    }
  }
}

Inputs ​

InputTypeDefaultDescription
appKeystringrequiredCaptchaLa application key
serverTokenstring-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
actionstring'default'Action identifier (e.g. login, register, checkout)
langstringbrowser languageLanguage code (zh-CN, en, ja, …); unset = the browser language. Changing it switches the language in place
themestring | objectfollows the systemPreset string or { custom: { brightness: 'light' | 'dark' | 'system', ... } } (see Web SDK options)
bindTostring | 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 to report the error

Changing appKey, product, action, bindTo, serverToken or theme rebuilds the widget.

Outputs ​

OutputPayloadDescription
successCaptchalaResult ({ token, type, action })Verification succeeded
errorerrorLoading or 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 template reference or viewChild) ​

ts
@Component({
  standalone: true,
  imports: [CaptchalaComponent],
  template: `
    <captchala #captcha appKey="your-app-key" />
    <button (click)="captcha.verify()">Verify</button>
  `
})
export class AppComponent {}
MethodDescription
verify()Trigger verification
reset()Reset CAPTCHA state
destroy()Destroy the instance
bind(selector)Bind to element (for bind mode). Named bind because bindTo is the input
setLang(lang)Switch language in place
getInstance()The underlying web SDK instance

injectCaptchala() / CaptchalaService ​

ts
const {
  ready,      // Signal<boolean>: the SDK is loaded and ready
  loading,    // Signal<boolean>: the SDK is loading
  error,      // Signal<Error | null>: loading or configuration error
  verify,     // () => Promise<CaptchalaResult>: start a verification
  reset,      // () => void
  bindTo,     // (selector) => void
  appendTo,   // (selector) => void
  setLang,    // (lang) => void
  destroy,    // () => void (done for you by injectCaptchala)
} = injectCaptchala(options)

injectCaptchala() must run in an injection context (a constructor or field initializer) and is destroyed with the component or service that created it. Elsewhere, use inject(CaptchalaService).create(options) and call destroy() yourself. CaptchalaService.load() preloads the SDK.

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(), or show your own prompt.

ts
@Component({
  standalone: true,
  imports: [CaptchalaComponent],
  template: `
    <captchala
      #captcha
      appKey="your-app-key"
      (success)="token.set($event.token); expired.set(false)"
      (expired)="token.set(''); expired.set(true)"
    />
    @if (expired()) {
      <button (click)="expired.set(false); captcha.reset(); captcha.verify()">Verify again</button>
    }
  `
})
export class FormComponent {
  token = signal('')
  expired = signal(false)
}

Without an (expired) listener, error fires once (class expired). expired never fires for offline or degraded tokens. injectCaptchala({ onExpired }) works the same way.

Server-side rendering ​

The component renders nothing on the server and loads the SDK only in the browser, after the view is created. injectCaptchala() stays idle on the server (ready() is false, verify() rejects). No extra configuration is needed for Angular SSR or prerendering.

Production: serverToken mode ​

For high-value flows (login, register, payment) we recommend the server-issued token flow. Your backend mints a one-time server_token (5-minute TTL) via POST /v1/server/challenge/issue and hands it to the browser:

ts
import { Component, inject, signal } from '@angular/core'
import { HttpClient } from '@angular/common/http'
import { CaptchalaComponent } from '@captcha-la/angular'

@Component({
  selector: 'app-login-page',
  standalone: true,
  imports: [CaptchalaComponent],
  template: `
    @if (serverToken()) {
      <captchala
        appKey="pk_your_public_app_key"
        action="login"
        [serverToken]="serverToken()"
        (success)="onSuccess($event.token)"
      />
    } @else {
      <div>Loading…</div>
    }
  `
})
export class LoginPageComponent {
  serverToken = signal<string | undefined>(undefined)

  constructor() {
    inject(HttpClient)
      .get<{ server_token: string }>('/api/captcha-token')   // your own backend
      .subscribe((d) => this.serverToken.set(d.server_token))
  }

  onSuccess(passToken: string) {
    console.log('pass_token:', passToken)
  }
}

The @if block syntax needs Angular 17; on Angular 16 use *ngIf.

  • Never embed the app secret in browser code.
  • Each server_token can be used exactly once. Fetch a new one before every widget mount.
  • Apps with server_token_required=false can still use the public appKey-only flow.

With a Node.js backend, the Node.js SDK issues the token (issueServerToken). See API Reference for the full backend contract.

Server-side validation ​

Send the token from success to your backend and validate it there. With the Node.js SDK:

ts
import { CaptchaLaClient } from '@captcha-la/node'

const captchala = new CaptchaLaClient() // CAPTCHALA_APP_KEY / CAPTCHALA_APP_SECRET

const result = await captchala.validate(token, { clientIp: req.ip })
if (result.valid) {
  // Token is valid, proceed with the request
}

Or call the API directly:

js
const response = await fetch('https://verify.captcha.la/v1/validate', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'X-App-Key': process.env.CAPTCHALA_APP_KEY,
    'X-App-Secret': process.env.CAPTCHALA_APP_SECRET
  },
  // client_ip is optional but recommended
  body: JSON.stringify({ pass_token: token, client_ip: req.ip })
})

const result = await response.json()
if (result.code === 0 && result.data.valid === true) {
  // Token is valid, proceed with the request
}

TypeScript ​

ts
import {
  CaptchalaComponent,
  CaptchalaService,
  injectCaptchala,
  type CaptchalaResult,
  type CaptchalaHandle,
  type CaptchalaHandleOptions
} from '@captcha-la/angular'

Versions ​

  • 1.0.1 (2026-11-27): README in Japanese, Korean, Spanish, Portuguese (BR) and German; no code changes.
  • 1.0.0: first public release.

MIT-licensed examples · CaptchaLa is operated independently