All documentation

Integration Examples

Node.js, Python, cURL, and webhook handler examples.

Code Examples

Complete integration examples showing how to submit events and handle decisions in your application.

const axios = require("axios");

async function submitEvent(eventData) {
  try {
    const response = await axios.post(
      "https://api.naiza.ai/api/v1/events",
      eventData,
      {
        headers: {
          "x-api-key": process.env.NAIZA_API_KEY,
          "Content-Type": "application/json",
        },
      },
    );

    const { decision, score } = response.data;

    if (decision === "BLOCK") {
      throw new Error("Access denied due to fraud detection");
    }

    if (decision === "REVIEW") {
      return { require2FA: true, score };
    }

    return { allowed: true, score };
  } catch (error) {
    console.error("Fraud check failed:", error);
    return { allowed: true }; // Fail open
  }
}

// Usage in login endpoint
app.post("/login", async (req, res) => {
  const { email, password } = req.body;
  const user = await authenticateUser(email, password);

  const fraudCheck = await submitEvent({
    eventName: "user.login",
    eventCategory: "AUTHENTICATION",
    customer: {
      externalId: user.id,
      email: email,
    },
    device: {
      fingerprint: req.body.deviceFingerprint,
    },
    ip: req.ip,
    userAgent: req.headers["user-agent"],
    sessionId: req.sessionID,
    metadata: {
      login_method: "password",
      auth_result: "success",
    },
  });

  if (fraudCheck.require2FA) {
    return res.json({ nextStep: "2fa_required" });
  }

  if (!fraudCheck.allowed) {
    return res.status(403).json({ error: "Access denied" });
  }

  res.json({ token: sessionToken });
});