Ir al contenido

App Next.js completa

Existe una app de referencia (cortexid-sdk-example) con la integración completa: un botón “Iniciar catastro” que pide un token de sesión a su backend, monta el SDK y muestra el JSON enviado y el resultante. Este es el corazón, resumido:

const SDK_ORIGIN = process.env.NEXT_PUBLIC_CORTEXID_SDK_ORIGIN ?? 'https://sdk.cortexid.uk';
useEffect(() => {
const script = document.createElement('script');
script.src = `${SDK_ORIGIN}/v1/cortexid-sdk.js`;
script.async = true;
script.onload = () => setLoaderReady(true);
document.body.appendChild(script);
return () => {
flowRef.current?.unmount();
script.remove();
};
}, []);

La API key vive solo del lado del servidor. Una route handler la usa para pedirle una sesión de vida corta a CortexID (POST /api/v1/sdk/sessions) y devuelve el token al cliente — nunca la API key:

// app/api/cortexid-session/route.ts (server-only)
const CORTEXID_API = process.env.CORTEXID_API_BASE_URL ?? 'https://cortexid.uk';
export async function POST(req: Request) {
const { action, userId } = await req.json();
const r = await fetch(`${CORTEXID_API}/api/v1/sdk/sessions`, {
method: 'POST',
headers: {
'X-API-Key': process.env.CORTEXID_API_KEY!, // solo del lado del server
'Content-Type': 'application/json',
},
body: JSON.stringify({ action, user_id: userId }),
});
if (!r.ok) {
return Response.json({ error: 'session_failed' }, { status: 502 });
}
const { session, expires_at } = await r.json();
return Response.json({ session, expires_at }); // el cliente recibe el token, no la key
}
interface CortexIDFlow { iframe: HTMLIFrameElement; unmount: () => void }
interface CortexIDGlobal { mount(opts: Record<string, unknown>): CortexIDFlow }
declare global {
interface Window { CortexID?: CortexIDGlobal }
}
async function iniciarCatastro() {
// pedí el token a TU backend (paso 2)
const { session } = await fetch('/api/cortexid-session', {
method: 'POST',
body: JSON.stringify({ action: 'enroll', userId }),
}).then((r) => r.json());
const config = {
action: 'enroll',
userId,
session,
params: { document: true, branding: { brand: 'CORTEX·ID · MI EMPRESA' } },
};
setSentJson(config); // ← "JSON enviado"
flowRef.current = window.CortexID!.mount({
container: containerRef.current!,
...config,
onReady: () => log('ready'),
onStep: (ev) => log(`step → ${ev.step}`),
onSuccess: (result) => setResultJson(result), // ← "JSON resultante"
onError: (e) => log(`error → ${e.message}`),
onClose: () => flowRef.current?.unmount(),
});
}
<button onClick={iniciarCatastro} disabled={!loaderReady}>
Iniciar catastro
</button>
<div ref={containerRef} style={{ width: 420, height: 720 }} />

JSON enviado (tu configuración):

{
"action": "enroll",
"userId": "99476008",
"session": "cxs_…",
"params": { "document": true, "branding": { "brand": "CORTEX·ID · MI EMPRESA" } }
}

JSON resultante (al completar el flujo):

{
"userId": "99476008",
"quality": "high",
"document": {
"documentId": "01J…",
"verificationStatus": "auto_verified",
"documentNumber": "99476008",
"matchesUserId": true,
"data": { "fullName": "JUAN PÉREZ", "birthDate": "1990-01-15" }
}
}
Ventana de terminal
cd cortexid-sdk-example
npm install
cp .env.local.example .env.local # setea CORTEXID_API_KEY (server-only) y el origen del SDK
npm run dev # http://localhost:3020

Con NEXT_PUBLIC_CORTEXID_SDK_ORIGIN apuntando a producción (https://sdk.cortexid.uk) o a tu instancia local del SDK.