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

# Authentication

> API key management, environment variables, rate limits, और security best practices।

सभी RAIL API requests API keys से authenticate होती हैं। Keys आपके account से linked होती हैं, आपका credit balance carry करती हैं, और आपके usage logs में दिखती हैं।

```mermaid theme={null}
flowchart LR
    App["आपका Application"] -->|"Authorization: Bearer rail_..."| API["RAIL API"]
    API -->|"200 + result"| App
    API -->|"401 Invalid key"| App
    API -->|"429 Rate limit"| App
```

## API key लेना

<Steps>
  <Step title="Sign up करें">
    [responsibleailabs.ai पर एक free account बनाएँ](https://responsibleailabs.ai/register)।
  </Step>

  <Step title="Dashboard खोलें">
    अपने [Dashboard](https://responsibleailabs.ai/dashboard) पर जाएँ और **API Keys** section में जाएँ।
  </Step>

  <Step title="Key generate करें">
    **Generate Key** पर click करें। Keys `rail_` से शुरू होती हैं और सिर्फ़ एक बार दिखती हैं। तुरंत copy करें।

    <Warning>
      Dialog बंद करने के बाद key दोबारा नहीं दिखेगी। Page छोड़ने से पहले इसे securely store करें।
    </Warning>
  </Step>
</Steps>

## API key use करना

हर request पर `Authorization` header में अपनी key Bearer token के रूप में pass करें:

```
Authorization: Bearer YOUR_RAIL_API_KEY
```

Full cURL example:

```bash theme={null}
curl -X POST https://api.responsibleailabs.ai/railscore/v1/eval \
  -H "Authorization: Bearer YOUR_RAIL_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"content": "Your AI-generated text here", "mode": "basic"}'
```

## SDK authentication

दोनों SDKs client बनाते समय API key accept करते हैं और automatically header attach कर देते हैं:

<CodeGroup>
  ```python Python theme={null}
  from rail_score_sdk import RailScoreClient
  import os

  # Key directly pass करें (scripts के लिए ठीक है)
  client = RailScoreClient(api_key="YOUR_RAIL_API_KEY")

  # Recommended: environment variable से पढ़ें
  client = RailScoreClient(api_key=os.environ["RAIL_API_KEY"])
  ```

  ```typescript JavaScript theme={null}
  import { RailScoreClient } from "@responsible-ai-labs/rail-score";

  // Key directly pass करें
  const client = new RailScoreClient({ apiKey: "YOUR_RAIL_API_KEY" });

  // Recommended: environment variable से पढ़ें
  const client = new RailScoreClient({ apiKey: process.env.RAIL_API_KEY });
  ```
</CodeGroup>

## Environment variables

Source files में कभी API keys hardcode मत करें। Environment variables use करें और keys को version control से बाहर रखें।

<Tabs>
  <Tab title="Local (dotenv)">
    ```bash theme={null}
    # .env  — .gitignore में add करें, इस file को कभी commit मत करें
    RAIL_API_KEY=YOUR_RAIL_API_KEY
    ```

    ```python theme={null}
    from dotenv import load_dotenv
    import os

    load_dotenv()
    api_key = os.environ["RAIL_API_KEY"]
    ```
  </Tab>

  <Tab title="Vercel / Edge">
    **Vercel Dashboard > Settings > Environment Variables** से add करें।

    Variable name: `RAIL_API_KEY`

    <Warning>
      Variable को कभी `NEXT_PUBLIC_` prefix मत दें। इससे यह browser bundle में expose हो जाएगा।
    </Warning>

    ```typescript theme={null}
    // Server-side only
    const apiKey = process.env.RAIL_API_KEY;
    ```
  </Tab>

  <Tab title="Docker">
    ```bash theme={null}
    # Runtime पर pass करें — image layers में कभी secrets मत bake करें
    docker run -e RAIL_API_KEY=YOUR_RAIL_API_KEY my-app

    # docker-compose.yml
    services:
      app:
        image: my-app
        environment:
          - RAIL_API_KEY=${RAIL_API_KEY}
    ```
  </Tab>
</Tabs>

## Key management

आप अलग-अलग environments (production, staging, CI) के लिए multiple keys बना सकते हैं। अपने [Dashboard](https://responsibleailabs.ai/dashboard) से सभी keys manage करें।

| Action           | कब use करें                                                                      |
| ---------------- | -------------------------------------------------------------------------------- |
| **Generate key** | नया environment, नई service onboard करना, या schedule पर credentials rotate करना |
| **Rename key**   | Usage tracking के लिए "production-chatbot" या "staging-ci" जैसा context add करें |
| **Revoke key**   | Key leak का शक, employee offboarding, या service बंद करना। तुरंत effect होता है। |

<Tip>
  **Zero-downtime rotation:** नई key generate करें, अपनी service में deploy करें और verify करें कि काम कर रही है, फिर पुरानी key revoke करें। Swap में seconds लगते हैं।
</Tip>

## Rate limits

Rate limits per API key apply होती हैं। Limit exceed करने पर HTTP 429 return होता है।

| Plan       | Requests / min | Requests / day |
| ---------- | -------------- | -------------- |
| Free       | 10             | 100            |
| Pro        | 60             | 5,000          |
| Business   | 300            | 50,000         |
| Enterprise | Custom         | Custom         |

## Security best practices

<Warning>
  * **Client-side code में कभी keys expose मत करें।** Browsers public हैं। हमेशा API को अपने backend या serverless function से call करें।
  * **Keys को environment variables में store करें,** source files या Docker images में कभी hardcode मत करें।
  * **पहले commit से पहले `.gitignore` में `.env` add करें** ताकि keys कभी accidentally push ना हों।
  * **हर environment के लिए अलग keys use करें।** Staging compromise हो तो भी production safe रहे।
  * **Expose होने पर तुरंत revoke करें।** पहले replacement generate करें, फिर revoke करें।
  * **Schedule पर rotate करें।** समय-समय पर keys regenerate करना किसी भी undetected leak का blast radius limit करता है।
</Warning>

## Auth error responses

| Status | Error                                     | Fix                                                                 |
| ------ | ----------------------------------------- | ------------------------------------------------------------------- |
| `401`  | Authorization header missing या malformed | `Authorization: Bearer …` add करें                                  |
| `401`  | Invalid API key                           | Verify करें कि key `rail_` से शुरू होती है और correctly copy हुई है |
| `403`  | API key revoked या inactive               | Dashboard से नई key generate करें                                   |
| `429`  | Rate limit exceeded                       | Request frequency कम करें या plan upgrade करें                      |

## आगे क्या करें

<CardGroup cols={2}>
  <Card title="Quickstart" icon="rocket" href="/getting-started/quickstart">
    5 मिनट से कम में अपनी पहली evaluation request करें।
  </Card>

  <Card title="Credits & Pricing" icon="coins" href="/getting-started/credits">
    समझें कि per call credits कैसे charge होते हैं।
  </Card>

  <Card title="API Reference" icon="code" href="/api-reference/overview">
    सभी endpoints का full parameter reference।
  </Card>

  <Card title="Dashboard" icon="chart-line" href="https://responsibleailabs.ai/dashboard">
    Keys manage करें, usage देखें, और credit balance monitor करें।
  </Card>
</CardGroup>
