Skip to main content

Getting Your API Key

  1. Visit Legnext.ai Dashboard
  2. Navigate to API Keys section
  3. Generate a new API key
  4. Copy and store securely

Authentication Methods

Set your API key as an environment variable:
export LEGNEXT_API_KEY="your-api-key-here"
Then use it in your Go code:
package main

import (
    "context"
    "fmt"
    "os"
    legnext "github.com/legnext-ai/sdks/sdks/go"
)

func main() {
    // Configure API client
    config := legnext.NewConfiguration()
    config.Servers = legnext.ServerConfigurations{
        {
            URL: "https://api.legnext.ai",
        },
    }

    // Add API key from environment variable
    apiKey := os.Getenv("LEGNEXT_API_KEY")
    config.AddDefaultHeader("x-api-key", apiKey)

    client := legnext.NewAPIClient(config)
    ctx := context.Background()

    // Now you can use the client
    // ...
}

Method 2: Direct Initialization

Hardcode the API key directly (not recommended for production):
package main

import (
    "context"
    legnext "github.com/legnext-ai/sdks/sdks/go"
)

func main() {
    config := legnext.NewConfiguration()
    config.Servers = legnext.ServerConfigurations{
        {
            URL: "https://api.legnext.ai",
        },
    }

    // Add API key directly
    config.AddDefaultHeader("x-api-key", "your-api-key-here")

    client := legnext.NewAPIClient(config)
    ctx := context.Background()

    // Use the client
    // ...
}

Method 3: Configuration File

Load API key from a configuration file:
package main

import (
    "context"
    "encoding/json"
    "fmt"
    "os"
    legnext "github.com/legnext-ai/sdks/sdks/go"
)

type Config struct {
    APIKey string `json:"api_key"`
}

func loadConfig(path string) (*Config, error) {
    file, err := os.Open(path)
    if err != nil {
        return nil, err
    }
    defer file.Close()

    var config Config
    decoder := json.NewDecoder(file)
    err = decoder.Decode(&config)
    if err != nil {
        return nil, err
    }

    return &config, nil
}

func main() {
    // Load configuration
    cfg, err := loadConfig("config.json")
    if err != nil {
        fmt.Printf("Error loading config: %v\n", err)
        return
    }

    // Configure API client
    config := legnext.NewConfiguration()
    config.Servers = legnext.ServerConfigurations{
        {
            URL: "https://api.legnext.ai",
        },
    }
    config.AddDefaultHeader("x-api-key", cfg.APIKey)

    client := legnext.NewAPIClient(config)
    ctx := context.Background()

    // Use the client
    // ...
}
config.json:
{
  "api_key": "your-api-key-here"
}

Method 4: Using godotenv

Use the godotenv package to load from .env files:
go get github.com/joho/godotenv
package main

import (
    "context"
    "fmt"
    "log"
    "os"

    legnext "github.com/legnext-ai/sdks/sdks/go"
    "github.com/joho/godotenv"
)

func main() {
    // Load .env file
    err := godotenv.Load()
    if err != nil {
        log.Fatal("Error loading .env file")
    }

    // Configure API client
    config := legnext.NewConfiguration()
    config.Servers = legnext.ServerConfigurations{
        {
            URL: "https://api.legnext.ai",
        },
    }

    apiKey := os.Getenv("LEGNEXT_API_KEY")
    if apiKey == "" {
        log.Fatal("LEGNEXT_API_KEY not set")
    }

    config.AddDefaultHeader("x-api-key", apiKey)

    client := legnext.NewAPIClient(config)
    ctx := context.Background()

    // Use the client
    // ...
}
.env file:
LEGNEXT_API_KEY=your-api-key-here

Security Best Practices

  1. Never commit API keys to version control
    • Add .env and config.json to .gitignore
    • Use environment variables in production
  2. Use environment variables in production
    # Set in your deployment environment
    export LEGNEXT_API_KEY="your-production-key"
    
  3. Rotate keys regularly
    • Generate new keys periodically
    • Revoke old keys after rotation
  4. Use different keys for development and production
    var apiKey string
    if os.Getenv("ENVIRONMENT") == "production" {
        apiKey = os.Getenv("LEGNEXT_PROD_API_KEY")
    } else {
        apiKey = os.Getenv("LEGNEXT_DEV_API_KEY")
    }
    
  5. Restrict API key permissions
    • Use the minimum required permissions
    • Monitor usage through the dashboard

Testing Authentication

Verify your API key is working:
package main

import (
    "context"
    "fmt"
    "os"
    legnext "github.com/legnext-ai/sdks/sdks/go"
)

func main() {
    config := legnext.NewConfiguration()
    config.Servers = legnext.ServerConfigurations{
        {
            URL: "https://api.legnext.ai",
        },
    }

    apiKey := os.Getenv("LEGNEXT_API_KEY")
    config.AddDefaultHeader("x-api-key", apiKey)

    client := legnext.NewAPIClient(config)
    ctx := context.Background()

    // Test with account balance endpoint
    balance, httpRes, err := client.AccountManagementAPI.ApiAccountBalanceGet(ctx).XApiKey(apiKey).Execute()

    if err != nil {
        if httpRes != nil && httpRes.StatusCode == 401 {
            fmt.Println("❌ Authentication failed: Invalid API key")
        } else {
            fmt.Printf("❌ Error: %v\n", err)
        }
        return
    }

    fmt.Println("✅ Authentication successful!")
    if balance.Data != nil {
        fmt.Printf("Balance: $%.2f\n", *balance.Data.BalanceUsd)
    }
}

Next Steps