# JavaScript over HTTP

Server-side fetch with status checking and a timeout, keeping the API key out of the browser.

Source: https://shvydko.ai/en/docs/integrations/javascript

## Run on the server

The example uses Node.js with built-in fetch and AbortSignal.timeout. Save it as request.mjs and take request.json from the quickstart. SHVYDKO_BASE_URL and SHVYDKO_API_KEY must be available to the server process.

In a web application, call Shvydko from your own protected server endpoint. Do not put the secret in a client React component, HTML, or a public bundle.

- [Copy request.json](https://shvydko.ai/en/docs/quickstart#request)

## Request with fetch

### request.mjs · server-side JavaScript

```javascript
import { readFile } from "node:fs/promises";

const baseUrl = process.env.SHVYDKO_BASE_URL?.replace(/\/$/, "");
const apiKey = process.env.SHVYDKO_API_KEY;
if (!baseUrl || !apiKey) throw new Error("Missing server configuration");
const payload = JSON.parse(await readFile("request.json", "utf8"));

let response;
try {
  response = await fetch(baseUrl + "/v1/systemone", {
    method: "POST",
    headers: {
      Authorization: "Bearer " + apiKey,
      "Content-Type": "application/json",
    },
    body: JSON.stringify(payload),
    signal: AbortSignal.timeout(150_000),
  });
} catch {
  throw new Error("Network failure; check Usage before retrying a paid request");
}

const body = await response.json().catch(() => null);
if (!response.ok) {
  const detail = typeof body?.detail === "string" ? body.detail : "Request failed";
  const retryAfter = response.headers.get("Retry-After");
  throw new Error(
    "Shvydko HTTP " + response.status + ": " + detail + "; Retry-After=" + retryAfter
  );
}
if (!body?.answers || !body?.usage || !body?.request_id) {
  throw new Error("Unexpected response shape");
}

console.log({
  requestId: body.request_id,
  team: body.answers.team.choice,
  inputTokens: body.usage.input_tokens,
  costUsd: body.usage.cost_usd,
});
```

## Integrate into a controlled workflow

Check the status before reading answers. For business actions, validate choice against an allowlist and numeric values against expected bounds. A network timeout on your client does not prove the server failed to execute the request.

The example requires no SDK and does not assume support for OpenAI chat/completions. Use the /v1/systemone contract.

## Related documentation

- [First request](https://shvydko.ai/en/docs-markdown/quickstart.md)
- [Keys and authentication](https://shvydko.ai/en/docs-markdown/api/authentication.md)
- [Errors and retries](https://shvydko.ai/en/docs-markdown/api/errors.md)
