Environment Variables
Learn how to manage environment variables for different deployment scenarios.
Overview
Using environment variables is the recommended way to configure Nuxt Typesense. This approach provides:
- Security: Keep sensitive data out of your codebase
- Flexibility: Easy configuration per environment
- Portability: Simple deployment across different platforms
Setup
1. Create Environment Files
Create .env files in your project root:
# .env - Default values (never commit this!)
TYPESENSE_URL=http://localhost:8108
TYPESENSE_API_KEY=xyz
# .env.example - Template for other developers
TYPESENSE_URL=http://localhost:8108
TYPESENSE_API_KEY=your-api-key-here2. Configure nuxt.config.ts
Reference environment variables in your configuration:
// nuxt.config.ts
export default defineNuxtConfig({
modules: ['@sfxcode/nuxt-typesense'],
typesense: {
url: process.env.TYPESENSE_URL,
apiKey: process.env.TYPESENSE_API_KEY,
}
})3. Update .gitignore
Ensure environment files are not committed:
# .gitignore
.env
.env.*
!.env.exampleEnvironment-Specific Configuration
Development
# .env.development
TYPESENSE_URL=http://localhost:8108
TYPESENSE_API_KEY=dev-key-xyz
TYPESENSE_ADMIN_KEY=dev-admin-keyStaging
# .env.staging
TYPESENSE_URL=https://staging.typesense.example.com
TYPESENSE_API_KEY=staging-search-key
TYPESENSE_ADMIN_KEY=staging-admin-keyProduction
# .env.production
TYPESENSE_URL=https://xxx.a1.typesense.net
TYPESENSE_API_KEY=prod-search-only-key
TYPESENSE_ADMIN_KEY=prod-admin-keyMultiple API Keys
For enhanced security, use different keys for different operations:
# .env
# Public search key (can be exposed to client in client mode)
TYPESENSE_SEARCH_KEY=search-only-key-xyz
# Server-only admin key for write operations and key management
TYPESENSE_ADMIN_KEY=admin-key-keep-secretConfigure the server or browser mode depending on where the key should live:
export default defineNuxtConfig({
modules: ['@sfxcode/nuxt-typesense'],
typesense: {
url: process.env.TYPESENSE_URL,
// Default server mode keeps this key private.
apiKey: process.env.TYPESENSE_ADMIN_KEY,
},
})For client-side-only searches, keep a search-only key in public runtime config instead:
export default defineNuxtConfig({
modules: ['@sfxcode/nuxt-typesense'],
typesense: {
url: process.env.TYPESENSE_URL,
apiKey: process.env.TYPESENSE_SEARCH_KEY,
clientMode: true,
},
})When you need to call privileged APIs from a server route, use the private runtime config and the server helper instead of the browser composables:
// server/api/admin/collections.get.ts
export default defineEventHandler(async (event) => {
const config = useRuntimeConfig(event)
const { collectionsApi } = useTypesenseServerApi(config.typesense.url, config.typesense.apiKey)
return collectionsApi.getCollections()
})Platform-Specific Setup
Vercel
Add environment variables in the Vercel dashboard or use Vercel CLI:
vercel env add TYPESENSE_URL
vercel env add TYPESENSE_API_KEYNetlify
Add variables in Netlify dashboard or use netlify.toml:
# netlify.toml
[build.environment]
TYPESENSE_URL = "https://xxx.a1.typesense.net"
[context.production.environment]
TYPESENSE_API_KEY = "production-key"
[context.deploy-preview.environment]
TYPESENSE_API_KEY = "preview-key"Docker
Pass environment variables when running the container:
docker run \
-e TYPESENSE_URL=http://typesense:8108 \
-e TYPESENSE_API_KEY=xyz \
your-nuxt-appOr use a .env file with Docker Compose:
# docker-compose.yml
version: '3'
services:
nuxt:
build: .
env_file:
- .env
ports:
- "3000:3000"GitHub Actions
Add secrets in repository settings, then use in workflows:
# .github/workflows/deploy.yml
name: Deploy
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Deploy
env:
TYPESENSE_URL: ${{ secrets.TYPESENSE_URL }}
TYPESENSE_API_KEY: ${{ secrets.TYPESENSE_API_KEY }}
run: |
npm install
npm run buildRuntime Configuration
For dynamic configuration that needs to be available at runtime:
// nuxt.config.ts
export default defineNuxtConfig({
runtimeConfig: {
// Private keys (server-only)
typesense: {
adminKey: process.env.TYPESENSE_ADMIN_KEY,
},
// Public keys (exposed to client)
public: {
typesense: {
url: process.env.TYPESENSE_URL,
searchKey: process.env.TYPESENSE_SEARCH_KEY,
}
}
}
})Access in your application:
<script setup lang="ts">
// Client-side
const config = useRuntimeConfig()
console.log(config.public.typesense.url)
// Server-side
const serverConfig = useRuntimeConfig()
console.log(serverConfig.typesense.adminKey) // Only available on server
</script>Best Practices
1. Never Commit Secrets
# ❌ Bad
typesense: {
apiKey: 'my-secret-key'
}
# ✅ Good
typesense: {
apiKey: process.env.TYPESENSE_API_KEY
}2. Use Search-Only Keys in Client
# Client-side (frontend)
TYPESENSE_SEARCH_KEY=search-only-key
# Server-side (backend)
TYPESENSE_ADMIN_KEY=admin-key-with-full-accessWhen clientMode: true, prefer TYPESENSE_SEARCH_KEY and never reuse the admin key. The module throws at build time if the configured key equals TYPESENSE_ADMIN_KEY or NUXT_TYPESENSE_ADMIN_KEY in client mode; it cannot detect other write-capable keys.
3. Validate Required Variables
// nuxt.config.ts
if (!process.env.TYPESENSE_URL) {
throw new Error('TYPESENSE_URL is required')
}
if (!process.env.TYPESENSE_API_KEY) {
throw new Error('TYPESENSE_API_KEY is required')
}
export default defineNuxtConfig({
// ... config
})4. Provide Examples
# .env.example
# Copy this file to .env and fill in your values
# Typesense Server URL (required)
TYPESENSE_URL=http://localhost:8108
# Typesense API Key (required)
TYPESENSE_API_KEY=your-api-key
# Admin Key (optional, for server-side operations)
TYPESENSE_ADMIN_KEY=your-admin-key5. Use Type-Safe Access
Create a typed config helper:
// utils/config.ts
export function getTypesenseConfig() {
const url = process.env.TYPESENSE_URL
const apiKey = process.env.TYPESENSE_API_KEY
if (!url || !apiKey) {
throw new Error('Missing Typesense configuration')
}
return { url, apiKey }
}Troubleshooting
Variables Not Loading
- Check file name (should be
.env) - Restart dev server after changes
- Verify file location (project root)
- Check for typos in variable names
Variables Undefined in Production
- Ensure variables are set in hosting platform
- Check build logs for warnings
- Verify variables are not prefixed with
NUXT_unless intended - Test with
console.log(process.env.VARIABLE_NAME)
Client-Side Access Issues
Public variables must be in runtimeConfig.public:
runtimeConfig: {
public: {
typesenseUrl: process.env.TYPESENSE_URL
}
}Next Steps
- Learn about Module Configuration
- Explore Collections Management
- Check API Reference