NUWAY API

Code examples

Every endpoint in the API reference has cURL, JavaScript and Python examples. This page collects complete examples you can adapt.

Server-side chat relay

A minimal server-side relay between your own chat interface and NUWAY (Node.js + Express). Your frontend talks to your server; only your server knows the API key.

// Node.js 18+. ES module: set "type": "module" in package.json (or name the file server.mjs).
import express from "express";
const app = express();
app.use(express.json());
app.post("/support/chat", async (req, res) => {
  const { message, sessionId, email } = req.body;
  let nuway, data;
  try {
    nuway = await fetch("https://ai.nuway.co/api/v1/external/chat", {
      method: "POST",
      headers: {
        "X-API-KEY": process.env.NUWAY_API_KEY,
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        message,
        session_id: sessionId || undefined,
        customer: email ? { email } : undefined,
      }),
      signal: AbortSignal.timeout(60_000),
    });
    data = await nuway.json();
  } catch {
    // timeout, network error or a non-JSON error page
    return res.status(502).json({ reply: "Sorry, something went wrong. Please try again." });
  }
  if (nuway.status === 429) {
    return res.status(503).json({ reply: "We're busy right now, please try again in a moment." });
  }
  if (nuway.status === 402) {
    return res.json({ reply: data.detail.message, askEmail: true });
  }
  if (!nuway.ok) {
    return res.status(502).json({ reply: "Sorry, something went wrong. Please try again." });
  }
  res.json({
    reply: data.reply,
    sessionId: data.session_id, // store it and send it back with the next message
    askEmail: data.ask_email,
  });
});
app.listen(3000);

How it works:

  • Your frontend sends the customer's message (and the sessionId it got last time) to your server.
  • Your server adds the API key and calls Send a message.
  • 429 and 402 are turned into friendly replies; a 402 message is written for the customer. See Errors and rate limits.

Chat helper functions

Reusable functions that start or continue a conversation.

# Start a conversation
curl https://ai.nuway.co/api/v1/external/chat \
  -H "X-API-KEY: $NUWAY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"message": "Hi, what do you offer?"}'
# Continue it with the session_id from the reply
curl https://ai.nuway.co/api/v1/external/chat \
  -H "X-API-KEY: $NUWAY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"message": "Do you offer office tours?", "session_id": "api_0a8a897f15f547a7839987037932a43b"}'
// Node.js 18+
async function askNuway(message, sessionId) {
  const res = await fetch("https://ai.nuway.co/api/v1/external/chat", {
    method: "POST",
    headers: {
      "X-API-KEY": process.env.NUWAY_API_KEY,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ message, session_id: sessionId }),
    signal: AbortSignal.timeout(60_000),
  });
  if (!res.ok) throw new Error(`NUWAY ${res.status}: ${JSON.stringify(await res.json())}`);
  return res.json(); // { session_id, reply, ... }
}
import os
import requests
def ask_nuway(message, session_id=None):
    r = requests.post(
        "https://ai.nuway.co/api/v1/external/chat",
        headers={"X-API-KEY": os.environ["NUWAY_API_KEY"]},
        json={"message": message, **({"session_id": session_id} if session_id else {})},
        timeout=60,
    )
    r.raise_for_status()
    return r.json()  # {"session_id": ..., "reply": ..., ...}

Read new leads

Fetch the highest-intent hot leads, for example to copy them into your CRM on a schedule.

curl "https://ai.nuway.co/api/v1/external/leads?rank=hot&sort=intent&limit=10" \
  -H "X-API-KEY: $NUWAY_API_KEY"
// Node.js 18+. Uses top-level await: save as .mjs (or set "type": "module").
const res = await fetch("https://ai.nuway.co/api/v1/external/leads?rank=hot&sort=intent&limit=10", {
  method: "GET",
  headers: {
    "X-API-KEY": process.env.NUWAY_API_KEY
  }
});
const data = await res.json();
import os
import requests
r = requests.get(
    "https://ai.nuway.co/api/v1/external/leads?rank=hot&sort=intent&limit=10",
    headers={"X-API-KEY": os.environ["NUWAY_API_KEY"]},
    timeout=30,
)
r.raise_for_status()
data = r.json()

See List leads for every filter and field.