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.
Getting Your API Key
- Visit Legnext.ai Dashboard
- Navigate to API Keys section
- Generate a new API key
- Copy and store securely
Authentication Methods
Method 1: Environment Variables (Recommended)
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
-
Never commit API keys to version control
- Add
.env and config.json to .gitignore
- Use environment variables in production
-
Use environment variables in production
# Set in your deployment environment
export LEGNEXT_API_KEY="your-production-key"
-
Rotate keys regularly
- Generate new keys periodically
- Revoke old keys after rotation
-
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")
}
-
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