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

# Authentication

> Use API keys to authenticate API requests.

The Moonbase API uses API keys to authenticate all requests. An API key is required for all requests.

## How authentication works

API requests require valid API keys. The API returns specific errors based on authentication issues:

* **Invalid request error**: Missing or invalid API key
* **Authentication error**: Deleted or expired API key

For more details about handling authentication errors, see the Moonbase [error handling guide](/api-reference/error-handling).

## Manage API keys

Manage API keys in [organization settings](https://app.moonbase.ai/settings/api_keys).

<Warning>
  Keep API keys secret. Avoid client-side exposure.
</Warning>

## Using API key

### SDK authentication

SDKs check for an API key in the `MOONBASE_API_KEY` environment variable. Override this default when creating a client.

<CodeGroup>
  ```typescript TypeScript theme={null}
  import Moonbase from '@moonbaseai/sdk';

  const client = new Moonbase({
    apiKey: process.env['MOONBASE_API_KEY'], // This is the default and can be omitted
  });
  ```

  ```go Go theme={null}
  package main

  import (
  	"context"
  	"fmt"
  	"os"

  	"github.com/moonbaseai/moonbase-sdk-go"
  	"github.com/moonbaseai/moonbase-sdk-go/option"
  )

  func main() {
  	client := moonbase.NewClient(
  		option.WithApiKey(os.LookupEnv("MOONBASE_API_KEY"), // This is the default and can be omitted
  	)
  }
  ```

  ```ruby Ruby theme={null}
  require "bundler/setup"
  require "moonbase"

  moonbase = Moonbase::Client.new(
    api_key: ENV["MOONBASE_API_KEY"] # This is the default and can be omitted
  )
  ```

  ```python Python theme={null}
  import os
  from moonbase import Moonbase

  client = Moonbase(
      api_key=os.environ.get("MOONBASE_API_KEY"),  # This is the default and can be omitted
  )
  ```
</CodeGroup>

### Direct API calls

When calling the API directly, use HTTP Bearer authentication:

```sh theme={null}
curl --request GET \
  --url https://api.moonbase.ai/v0/collections/people \
  --header 'Authorization: Bearer MOONBASE_API_KEY'
```

## Best practices

* Never hardcode API keys
* Use different API keys for different environments (development, staging, production)
* Regenerate suspected compromised API keys
* Never log API keys in errors or debug output
