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

# Node.js SDK Quickstart

> Get started with the Centure Node.js SDK for prompt injection detection

The `@centure/node-sdk` package provides type-safe TypeScript support for detecting prompt injection attacks in your Node.js applications.

## Installation

<Steps>
  <Step title="Install the package">
    Install the SDK using your preferred package manager:

    <CodeGroup>
      ```bash npm theme={null}
      npm install @centure/node-sdk
      ```

      ```bash yarn theme={null}
      yarn add @centure/node-sdk
      ```

      ```bash pnpm theme={null}
      pnpm add @centure/node-sdk
      ```
    </CodeGroup>
  </Step>

  <Step title="Get your API key">
    Obtain your API key from the [Centure dashboard](https://app.centure.ai/api-keys). Store it securely in your environment variables.

    <Note>
      Never commit API keys to version control. Use environment variables or a secrets manager.
    </Note>
  </Step>

  <Step title="Initialize the client">
    Create a client instance with your API key:

    ```typescript theme={null}
    import { CentureClient } from "@centure/node-sdk";

    // Using environment variable
    const client = new CentureClient();

    // Or provide API key directly
    const client = new CentureClient({
      apiKey: "your-api-key",
      baseUrl: "https://api.centure.ai", // Optional
    });
    ```

    <Tip>
      Set the `CENTURE_API_KEY` environment variable to avoid passing the API key in code.
    </Tip>
  </Step>
</Steps>

## Scan Text Content

Detect prompt injection attempts in text messages, user inputs, or prompts:

```typescript theme={null}
const result = await client.scanText("Your text content here");

console.log("Is safe:", result.is_safe);
console.log("Detected categories:", result.categories);
console.log("Request ID:", result.request_id);
```

### Response Example

```json theme={null}
{
  "is_safe": false,
  "categories": [
    {
      "code": "context_injection",
      "confidence": "high"
    }
  ],
  "request_id": "req_abc123",
  "api_key_id": "key_xyz789",
  "request_units": 1,
  "service_tier": "standard"
}
```

## Scan Images

Detect text-based prompt injection in images using base64 encoding or Buffer objects:

<Tabs>
  <Tab title="Base64 String">
    ```typescript theme={null}
    const base64Image = "iVBORw0KGgoAAAANSUhEUgAA...";
    const result = await client.scanImage(base64Image);

    console.log("Is safe:", result.is_safe);
    console.log("Detected threats:", result.categories);
    ```
  </Tab>

  <Tab title="Buffer (Node.js)">
    ```typescript theme={null}
    import fs from "fs";

    const imageBuffer = fs.readFileSync("./image.png");
    const result = await client.scanImage(imageBuffer);

    console.log("Is safe:", result.is_safe);
    ```
  </Tab>
</Tabs>

<Info>
  The SDK automatically converts Buffer objects to base64 format for transmission.
</Info>

## Error Handling

Handle specific error scenarios with typed error classes:

```typescript theme={null}
import {
  CentureClient,
  BadRequestError,
  UnauthorizedError,
  PayloadTooLargeError,
  MissingApiKeyError,
} from "@centure/node-sdk";

try {
  const result = await client.scanText("content");
} catch (error) {
  if (error instanceof UnauthorizedError) {
    console.error("Invalid API key");
  } else if (error instanceof BadRequestError) {
    console.error("Invalid request:", error.message);
  } else if (error instanceof PayloadTooLargeError) {
    console.error("Image exceeds size limit");
  } else if (error instanceof MissingApiKeyError) {
    console.error("API key not provided");
  }
}
```

## Next Steps

<CardGroup cols={2}>
  <Card title="API Reference" icon="book" href="/sdk/nodejs-reference">
    Explore response types, threat categories, and advanced configuration options
  </Card>

  <Card title="MCP Integration" icon="plug" href="/sdk/nodejs-mcp">
    Secure Model Context Protocol applications with automatic scanning
  </Card>

  <Card title="npm Package" icon="npm" href="https://www.npmjs.com/package/@centure/node-sdk">
    View package details and version history on npm
  </Card>

  <Card title="GitHub Repository" icon="github" href="https://github.com/centure-ai/typescript-sdk">
    View source code, report issues, and contribute
  </Card>
</CardGroup>
