# Deno + OpenAI

The official OpenAI JavaScript SDK runs through Deno's npm compatibility layer. New text-generation projects should start with the Responses API.

```bash
deno add npm:openai
```

```ts title="main.ts"
import OpenAI from "openai";

const model = Deno.env.get("OPENAI_MODEL");
if (!model) throw new Error("OPENAI_MODEL is required");

const client = new OpenAI(); // reads OPENAI_API_KEY by default
const response = await client.responses.create({
  model,
  instructions: "Answer accurately and say when evidence is missing.",
  input: "Explain Deno permissions in two sentences.",
});

console.log(response.output_text);
```

```bash
OPENAI_MODEL=<a-verified-model-name> \
OPENAI_API_KEY=<secret> \
deno run --allow-env=OPENAI_MODEL,OPENAI_API_KEY \
  --allow-net=api.openai.com:443 main.ts
```

## Why the model is not hard-coded

Model availability, price, and capability change. Keep the model in deployment configuration, select it against your quality, latency, cost, and safety evals, and compare the same eval set before upgrading. A temporary example default should not become a production decision.

## Server boundary

- Keep the API key server-side; never send it to the browser.
- Record request ID, model, latency, token usage, and error class—not secrets or complete sensitive inputs.
- Set a deadline and cancel upstream work when the client disconnects.
- Treat model output as untrusted data; validate before rendering HTML, executing a tool, or writing a database.
- `response.output` can contain several item types; use the SDK's `output_text` aggregate when you only need text.

Official references: [OpenAI text generation](https://developers.openai.com/api/docs/guides/text), [OpenAI JavaScript SDK](https://github.com/openai/openai-node), and [Deno AI examples](https://docs.deno.com/examples/?category=ai).
