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

# Autentikasi

> Amankan permintaan API Anda dengan autentikasi yang tepat

## Gambaran Umum

CSKU AI Public API menggunakan autentikasi Bearer Token dengan Secret Key.

<Callout type="warning">
  <strong>Catatan Keamanan:</strong> Jangan pernah mengekspos Secret Key Anda di kode sisi klien. Selalu lakukan panggilan API dari lingkungan server yang aman.
</Callout>

## Mendapatkan Secret Key

<Steps>
  <Step title="Login ke Dashboard">
    Buka [app.csku.ai](https://app.csku.ai) dan login ke akun Anda
  </Step>

  <Step title="Buka Secret Key Settings">
    Navigasi ke **Settings** → **Secret Key Settings**
  </Step>

  <Step title="Salin Secret Key">
    Salin Secret Key Anda
  </Step>
</Steps>

<CardGroup cols={1}>
  <Card title="Dapatkan Secret Key" icon="key" href="https://app.csku.ai/settings/secret-key-settings">
    Buka halaman Secret Key Settings untuk mendapatkan kredensial Anda
  </Card>
</CardGroup>

## Format Autentikasi

Gunakan header Authorization dengan Bearer token:

```http theme={null}
Authorization: Bearer {secret_key}
```

## Contoh Penggunaan

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST "https://api.csku.ai/v1/message" \
    -H "Authorization: Bearer sk_xxxxxxxxxxxxx" \
    -H "Content-Type: application/json" \
    -d '{
      "conversation_id": "conv_123456",
      "text": "Halo, saya butuh bantuan"
    }'
  ```

  ```javascript Node.js theme={null}
  const SECRET_KEY = 'sk_xxxxxxxxxxxxx'; // Dari dashboard

  const response = await fetch('https://api.csku.ai/v1/message', {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${SECRET_KEY}`,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      conversation_id: 'conv_123456',
      text: 'Halo, saya butuh bantuan'
    })
  });

  const data = await response.json();
  console.log(data);
  ```

  ```python Python theme={null}
  import requests

  SECRET_KEY = 'sk_xxxxxxxxxxxxx'  # Dari dashboard

  response = requests.post(
      'https://api.csku.ai/v1/message',
      headers={
          'Authorization': f'Bearer {SECRET_KEY}',
          'Content-Type': 'application/json'
      },
      json={
          'conversation_id': 'conv_123456',
          'text': 'Halo, saya butuh bantuan'
      }
  )

  print(response.json())
  ```

  ```php PHP theme={null}
  <?php
  $SECRET_KEY = 'sk_xxxxxxxxxxxxx'; // Dari dashboard

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 'https://api.csku.ai/v1/message');
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, [
      'Authorization: Bearer ' . $SECRET_KEY,
      'Content-Type: application/json'
  ]);
  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
      'conversation_id' => 'conv_123456',
      'text' => 'Halo, saya butuh bantuan'
  ]));
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

  $response = curl_exec($ch);
  curl_close($ch);

  echo $response;
  ```
</CodeGroup>

## Praktik Keamanan Terbaik

<Callout type="error">
  <strong>Panduan Keamanan Penting</strong>
</Callout>

1. **Jangan Pernah Mengekspos Secret Key**
   * Simpan di variabel lingkungan (environment variables)
   * Gunakan layanan manajemen rahasia (AWS Secrets Manager, Azure Key Vault, dll.)
   * Jangan pernah commit ke version control

2. **Gunakan HTTPS**
   * Selalu gunakan HTTPS untuk semua permintaan API
   * Jangan pernah kirim kredensial melalui HTTP

3. **Rotasi Kredensial**
   * Ubah secret key secara periodik
   * Segera ganti jika ada kebocoran

## Respons Error Autentikasi

Jika autentikasi gagal, Anda akan menerima respons error:

```json theme={null}
{
  "status": 0,
  "rc": 401,
  "error_msg": "Data tidak valid. Mohon periksa kembali parameter dan header"
}
```

### Penyebab Error Umum

| Masalah                        | Solusi                                                               |
| ------------------------------ | -------------------------------------------------------------------- |
| Header Authorization tidak ada | Tambahkan header `Authorization: Bearer {secret_key}`                |
| Secret Key tidak valid         | Periksa kembali Secret Key dari dashboard                            |
| Format header salah            | Pastikan format: `Bearer {secret_key}` (dengan spasi setelah Bearer) |

## Batas Rate

API menerapkan pembatasan rate:

* **100 permintaan per menit** untuk permintaan terautentikasi

Ketika melebihi batas:

```json theme={null}
{
  "status": 0,
  "rc": 429,
  "error_msg": "Terlalu banyak permintaan. Silakan coba lagi nanti."
}
```

<Callout type="info">
  Implementasikan exponential backoff untuk menangani batas rate dengan baik.
</Callout>
