Skip to content

Environment Variables

SPEAR uses environment variables for configuration, allowing flexible deployment across different environments. Variables can be set via .env files, system environment, or systemd service configuration.

Required - AES encryption key for sensitive data.

Terminal window
SPEAR_ENCRYPTION_KEY=your32characterencryptionkey1234
RequirementDetails
LengthMust be exactly 16, 24, or 32 characters
PurposeEncrypts API keys and sensitive data in the database
RecommendationUse 32 characters for AES-256 encryption

Validation: The application validates key length on startup and will fail if invalid:

Encryption key must be 16, 24, or 32 characters long. Current length: X

Generate a secure key:

Terminal window
# Generate 32-character key (recommended)
openssl rand -base64 24
# Or using /dev/urandom
head -c 32 /dev/urandom | base64 | head -c 32

Application port for the HTTP server.

Terminal window
SPEAR_PORT=8090
Default8090
Range1-65535

Software version identifier (typically set during build).

Terminal window
SPEAR_VERSION=1.0.0

All OpenAI variables are optional and enable AI-powered features like text generation and grammar checking.

API key for OpenAI services.

Terminal window
OPENAI_API_KEY=sk-...

Get your key at: https://platform.openai.com/api-keys

Model selection for AI features.

Terminal window
OPENAI_MODEL=gpt-3.5-turbo
Defaultgpt-3.5-turbo
Optionsgpt-4, gpt-4-turbo, gpt-3.5-turbo

Custom endpoint URL for OpenAI-compatible APIs (e.g., Azure OpenAI).

Terminal window
OPENAI_API_URL=https://api.openai.com/v1/chat/completions
Defaulthttps://api.openai.com/v1/chat/completions

Maximum tokens for AI responses.

Terminal window
OPENAI_MAX_TOKENS=1000
Default1000

Controls creativity/randomness of AI responses.

Terminal window
OPENAI_TEMPERATURE=0.7
Default0.7
Range0.0 (focused) to 1.0 (creative)

These variables are used for development and testing environments.

Admin email for seeding development data.

Terminal window
SPEAR_ADMIN_EMAIL=admin@example.com

Admin password for seeding development data.

Terminal window
SPEAR_ADMIN_PASSWORD=yourpassword

These variables are required when building SPEAR from source.

Required for building - Public key for update signature verification.

Terminal window
UPDATER_PUBLIC_KEY=<base64-encoded-public-key>

Extract from your private key:

Terminal window
go run scripts/extract-public-key.go <private_key_b64>

Set to 0 for static builds.

Terminal window
CGO_ENABLED=0

Cross-compilation targets.

Terminal window
GOOS=linux
GOARCH=amd64
GOOSGOARCH Options
linuxamd64, arm64, arm
windowsamd64, arm64
darwinamd64, arm64

Create a .env file in the project root:

Terminal window
# Copy example and edit
cp .env.example .env
nano .env

Example .env file:

Terminal window
# Required
SPEAR_ENCRYPTION_KEY=your32characterencryptionkey1234
# Application
SPEAR_PORT=8090
# OpenAI (optional)
OPENAI_API_KEY=sk-...
OPENAI_MODEL=gpt-3.5-turbo
OPENAI_MAX_TOKENS=1000
OPENAI_TEMPERATURE=0.7

Export variables in your shell profile (~/.bashrc, ~/.zshrc):

Terminal window
export SPEAR_ENCRYPTION_KEY="your32characterencryptionkey1234"
export SPEAR_PORT=8090

The deployment script automatically configures environment variables in the systemd service file at /etc/systemd/system/spear.service:

[Service]
Environment=SPEAR_ENCRYPTION_KEY=<your-key>
Environment=SPEAR_VERSION=<version>
Environment=SPEAR_PORT=<port>

To modify after deployment:

Terminal window
# Edit the service file
sudo systemctl edit spear
# Add overrides in the editor:
[Service]
Environment=OPENAI_API_KEY=sk-...
# Reload and restart
sudo systemctl daemon-reload
sudo systemctl restart spear

Here’s a complete .env file for production:

Terminal window
# =============================================================================
# Application Settings (Required)
# =============================================================================
# AES encryption key - MUST be 16, 24, or 32 characters
# Generate with: openssl rand -base64 24
SPEAR_ENCRYPTION_KEY=your32characterencryptionkey1234
# Application port
SPEAR_PORT=8090
# =============================================================================
# OpenAI API Configuration (Optional - for AI-powered features)
# =============================================================================
# OpenAI API Key
# Get your key at: https://platform.openai.com/api-keys
OPENAI_API_KEY=sk-...
# Model selection (optional, defaults to gpt-3.5-turbo)
# Options: gpt-4, gpt-4-turbo, gpt-3.5-turbo
OPENAI_MODEL=gpt-3.5-turbo
# Custom endpoint URL (optional, for Azure OpenAI or compatible APIs)
OPENAI_API_URL=https://api.openai.com/v1/chat/completions
# Max tokens (optional, defaults to 1000)
OPENAI_MAX_TOKENS=1000
# Temperature - Lower = more focused, Higher = more creative
# Range: 0.0 to 1.0 (optional, defaults to 0.7)
OPENAI_TEMPERATURE=0.7

Add to .gitignore:

.env
.env.local
.env.production

Always use 32 characters for maximum security (AES-256):

Terminal window
# Generate secure key
openssl rand -base64 24
Terminal window
# Restrict .env file permissions
chmod 600 .env
# For systemd service overrides
sudo chmod 600 /etc/systemd/system/spear.service.d/*.conf

When rotating the encryption key:

  1. Export encrypted data
  2. Update the encryption key
  3. Re-encrypt sensitive data
  4. Restart the service

Use separate files for different environments:

.env # Local development
.env.staging # Staging environment
.env.production # Production (never commit)

Verify your environment configuration:

Terminal window
# Check encryption key length
echo -n "$SPEAR_ENCRYPTION_KEY" | wc -c
# Verify OpenAI connectivity (if configured)
curl https://api.openai.com/v1/models \
-H "Authorization: Bearer $OPENAI_API_KEY"
# Check loaded environment in running service
sudo systemctl show spear --property=Environment
Error: Encryption key must be 16, 24, or 32 characters long

Solution: Check key length and regenerate if necessary:

Terminal window
# Check current length
echo -n "$SPEAR_ENCRYPTION_KEY" | wc -c
# Generate new 32-character key
export SPEAR_ENCRYPTION_KEY=$(openssl rand -base64 24)
Error: Failed to connect to OpenAI API

Solutions:

  1. Verify API key is valid
  2. Check network connectivity
  3. Verify custom URL if using OPENAI_API_URL

Solution: Ensure variables are in the systemd service file:

Terminal window
# Check current service environment
sudo systemctl show spear --property=Environment
# Reload after changes
sudo systemctl daemon-reload
sudo systemctl restart spear