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

# Authentication

> Set up authentication for the Legnext Rust SDK

## Get Your API Key

1. Visit [Legnext Dashboard](https://dashboard.legnext.ai)
2. Log in to your account
3. Navigate to **API Keys** section
4. Create a new API key
5. Copy and save it securely

## Using Your API Key

### Method 1: Direct Initialization

```rust theme={null}
use legnext_rust_sdk::apis::configuration::Configuration;
use legnext_rust_sdk::apis::image_api;

#[tokio::main]
async fn main() {
    let mut config = Configuration::new();
    config.base_path = "https://api.legnext.ai".to_string();

    let api_key = "your-api-key-here";
    // Use api_key in function calls
}
```

### Method 2: Environment Variable (Recommended)

Set the environment variable:

```bash theme={null}
# macOS/Linux
export LEGNEXT_API_KEY="your-api-key-here"

# Windows CMD
set LEGNEXT_API_KEY=your-api-key-here

# Windows PowerShell
$env:LEGNEXT_API_KEY="your-api-key-here"
```

Then in your code:

```rust theme={null}
use std::env;
use legnext_rust_sdk::apis::configuration::Configuration;

#[tokio::main]
async fn main() {
    let mut config = Configuration::new();
    config.base_path = "https://api.legnext.ai".to_string();

    let api_key = env::var("LEGNEXT_API_KEY")
        .expect("LEGNEXT_API_KEY environment variable not set");

    // Use api_key in function calls
}
```

### Method 3: Using dotenv Crate

Add to `Cargo.toml`:

```toml theme={null}
[dependencies]
dotenv = "0.15"
legnext-rust-sdk = "1.0"
tokio = { version = "1", features = ["full"] }
```

Create `.env` file:

```env theme={null}
LEGNEXT_API_KEY=your-api-key-here
LEGNEXT_BASE_URL=https://api.legnext.ai
```

In your code:

```rust theme={null}
use dotenv::dotenv;
use std::env;
use legnext_rust_sdk::apis::configuration::Configuration;

#[tokio::main]
async fn main() {
    dotenv().ok();

    let mut config = Configuration::new();
    config.base_path = env::var("LEGNEXT_BASE_URL")
        .unwrap_or_else(|_| "https://api.legnext.ai".to_string());

    let api_key = env::var("LEGNEXT_API_KEY")
        .expect("LEGNEXT_API_KEY not found in environment");

    // Use api_key in function calls
}
```

## Security Best Practices

⚠️ **Never hardcode API keys in your code**

✅ **Always use environment variables or .env files**

✅ **Add `.env` to `.gitignore`**

✅ **Use `expect()` or proper error handling for missing keys**

✅ **Rotate keys regularly and revoke compromised ones**

***

## Next Steps

* [Quick Start](/sdk/rust/quickstart)
* [Image Generation API](/sdk/rust/image-generation)
* [Video Generation API](/sdk/rust/video-generation)
