> ## Documentation Index
> Fetch the complete documentation index at: https://aimp.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# JavaScript Examples

> Execute models from Node.js services and signed-in browser flows.

# JavaScript examples

Use API keys only in server-side JavaScript. Browser apps should rely on the
signed-in product session and short-lived access tokens.

## Node.js server

```javascript theme={null}
const gatewayUrl = process.env.AIMP_GATEWAY_URL.replace(/\/$/, "");

export async function runModel(text) {
  const response = await fetch(`${gatewayUrl}/api/runs`, {
    method: "POST",
    headers: {
      "X-API-Key": process.env.AIMP_API_KEY,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      model: "demo-model",
      mode: "execute",
      scope: "playground",
      input: { text },
      params: {},
    }),
  });

  const body = await response.json();
  if (!response.ok) {
    throw new Error(body?.error?.message || body?.detail || "AIMP request failed");
  }
  return body;
}
```

## Signed-in browser flow

When you are inside the product app, call the same-origin API proxy and use the
access token managed by the app:

```javascript theme={null}
await fetch("/api/runs", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${accessToken}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    model: "demo-model",
    mode: "execute",
    scope: "playground",
    input: { text: "Hello" },
    params: {},
  }),
});
```

Do not embed `AIMP_API_KEY` in browser JavaScript.
