Authentication
Lunar SDK uses API Key authentication. You can provide your API key via environment variable or pass it directly to the client.
Using Environment Variable
Set the LUNAR_API_KEY environment variable:
export LUNAR_API_KEY="your-api-key"
Then initialize the client without arguments:
from lunar import Lunar
client = Lunar() # Automatically uses LUNAR_API_KEY
import { Lunar } from "lunar";
const client = new Lunar(); // Automatically uses LUNAR_API_KEY
Passing Directly
Pass the API key to the client constructor:
from lunar import Lunar
client = Lunar(api_key="your-api-key")
import { Lunar } from "lunar";
const client = new Lunar({ apiKey: "your-api-key" });
JWT Authentication
Both SDKs also support JWT authentication:
client = Lunar(jwt="eyJhbG...")
const client = new Lunar({ jwt: "eyJhbG..." });
The SDK automatically sets the authentication header:
| Header | Format |
|---|
x-api-key | {your-api-key} |
Authorization | Bearer {jwt-token} (when using JWT) |
Error Handling
from lunar import Lunar, AuthenticationError
try:
client = Lunar() # No credentials
except AuthenticationError as e:
print(f"Authentication failed: {e}")
# "Authentication required. Either pass api_key parameter or set
# LUNAR_API_KEY environment variable."
import { Lunar, AuthenticationError } from "lunar";
try {
const client = new Lunar(); // No credentials
} catch (e) {
if (e instanceof AuthenticationError) {
console.log(`Authentication failed: ${e}`);
// "Authentication required. Either pass apiKey/jwt option or set
// LUNAR_API_KEY or LUNAR_JWT environment variable."
}
}
Getting Your API Key
- Log in to the PureAI Console
- Navigate to API Keys section
- Click Create New Key
- Copy and securely store your key
Keep your API key secure. Never commit it to version control or expose it in client-side code.
Best Practices
Use Environment Variables in Production
# .env file (don't commit this!)
LUNAR_API_KEY=your-api-key
# Your code
from lunar import Lunar
client = Lunar() # Reads from environment
// Your code
import { Lunar } from "lunar";
const client = new Lunar(); // Reads from environment
Rotate Keys Regularly
- Create new keys periodically
- Revoke old keys after rotation
- Use separate keys for development and production
Limit Key Permissions
- Use the minimum required permissions
- Create separate keys for different applications