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

# Prompts

> TypeScript API reference for Netra prompts. Fetch managed prompt versions by name and label from the Netra backend.

The Netra SDK exposes a `prompts` API that lets you:

* **Fetch managed prompts** – Retrieve prompt versions by name and label from the Netra backend
* **Use labels for versioning** – Target specific prompt versions such as `"production"`, `"staging"`, or any custom label
* **Integrate seamlessly** – Drop fetched prompts directly into your LLM calls with zero boilerplate

This page shows how to use `getPrompt` to fetch and use your managed prompts.

## Getting Started

The prompts client is initialized automatically when you call `Netra.init`. No additional flags are required as long as a valid OTLP endpoint and API key are configured.

```typescript theme={null}
import { Netra } from "netra-sdk";

await Netra.init({
  appName: "sample-app",
});
```

***

## getPrompt

Fetch a prompt version by name and label from the Netra backend.

```typescript Usage theme={null}
import { Netra } from "netra-sdk";

await Netra.init({
  appName: "sample-app",
});

// Fetch the production version of a prompt
const prompt = await Netra.prompts.getPrompt({ name: "welcome-message" });

// Fetch a specific label
const stagingPrompt = await Netra.prompts.getPrompt({
  name: "welcome-message",
  label: "staging",
});
```

```typescript Signature theme={null}
getPrompt(params: GetPromptParams): Promise<PromptResponse | null>
```

### Parameters

| Parameter | Type | Description |
| - | - | - |
| `name` | `string` | Name of the prompt to fetch. Required — an empty string returns `null`. |
| `label` | `string?` | Label of the prompt version to retrieve (e.g. `"production"`, `"staging"`). Defaults to `"production"`. |
| `useCache` | `boolean?` | When `true`, read and write the in-memory cache. Defaults to `false`. |
| `cacheTtl` | `number?` | Per-call cache TTL in seconds. When omitted with `useCache: true`, defaults to `60`. |

### Return Value

| Scenario | Return |
| - | - |
| Prompt found | `PromptResponse` (`Record<string, any>`) — the prompt version data from the Netra backend |
| `name` is empty or missing | `null` |
| Network / server error | `null` |
| Client not initialized | `null` |

> [!NOTE]
>
> `getPrompt` is async. Always `await` the call. Errors are logged and a safe fallback (`null`) is returned so your application keeps running.

***

## Caching

By default, every `getPrompt` call hits the backend. Pass `useCache: true` to serve repeated lookups from an in-memory TTL cache.

```typescript theme={null}
// First call fetches from the backend; the second is served from cache
const prompt = await Netra.prompts.getPrompt({
  name: "welcome-message",
  useCache: true,
});

const cached = await Netra.prompts.getPrompt({
  name: "welcome-message",
  useCache: true,
});
```

Cache keys include both `name` and `label`, so production and staging versions are stored separately. Failed lookups (`null`) are not cached.

### clearCache

Clear all cached prompt entries. The SDK also clears this cache when you call `Netra.shutdown()`.

```typescript theme={null}
Netra.prompts.clearCache();
```

**Returns:** `void`

***

## Configuration

### Required

`getPrompt` relies on the OTLP endpoint and API key configured via `Netra.init` or environment variables.

| Setting | Environment Variable | Description |
| - | - | - |
| OTLP endpoint | `NETRA_OTLP_ENDPOINT` | Base URL for the Netra backend. The prompts client strips a trailing `/telemetry` suffix automatically. |
| API key | `NETRA_API_KEY` | API key sent as the `x-api-key` header on every request. |

***

## Error Handling

`getPrompt` is designed to never throw. Errors are logged and `null` is returned so your application keeps running.

```typescript theme={null}
import { Netra } from "netra-sdk";

await Netra.init({ appName: "sample-app" });

const prompt = await Netra.prompts.getPrompt({ name: "onboarding-flow" });

let promptText: string;
if (!prompt) {
  // Handle missing prompt — use a hardcoded fallback
  promptText = "Welcome! How can I help you today?";
} else {
  promptText = prompt.messages ?? "";
}
```

Common error scenarios and their log messages:

| Scenario | Log Level | Message |
| - | - | - |
| Empty `name` argument | `ERROR` | `netra.prompts: name is required to fetch a prompt` |
| Client not initialized | `ERROR` | `netra.prompts: Prompts client is not initialized; cannot fetch prompt` |
| Network / HTTP error | `ERROR` | `netra.prompts: Failed to fetch prompt: <error>` |

***

## Complete Example

```typescript theme={null}
import { Netra } from "netra-sdk";
import OpenAI from "openai";

// 1. Initialize Netra
await Netra.init({
  appName: "chatbot-service",
  environment: "production",
});

// 2. Fetch a managed prompt
const systemPrompt = await Netra.prompts.getPrompt({ name: "chatbot-system" });

if (!systemPrompt) {
  throw new Error("Required prompt 'chatbot-system' not found in Netra");
}

// 3. Use the prompt in your LLM call
const client = new OpenAI();

const response = await client.chat.completions.create({
  model: "gpt-4o",
  messages: systemPrompt.messages ?? [],
});

console.log(response.choices[0].message.content);

// 4. Shut down cleanly
await Netra.shutdown();
```

## Next Steps

* [TypeScript SDK Overview](https://docs.getnetra.ai/sdk-reference/sdk/typescript.md) - Full TypeScript API reference
* [Models](https://docs.getnetra.ai/sdk-reference/models/typescript.md) - Fetch model details and pricing
* [Evaluation](https://docs.getnetra.ai/sdk-reference/evaluation/typescript.md) - Evaluate AI outputs


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.