Overview
shrtnr requires several environment variables to connect to external services and configure authentication. This guide covers all required and optional variables.Create a
.env.local file in your project root for local development. Never commit this file to version control.Required Variables
Database
Neon PostgreSQL connection stringFormat: Where to get it:
postgresql://[user]:[password]@[host]/[database]?sslmode=requireExample:- Go to Neon Console
- Select your project
- Navigate to Dashboard → Connection Details
- Copy the connection string (use pooled connection for serverless)
Redis Cache
Upstash Redis REST API endpointFormat: Where to get it:
https://[your-db].[region].upstash.ioExample:- Go to Upstash Console
- Select your Redis database
- Copy the “REST URL” from the REST API section
Upstash Redis REST API authentication tokenExample:Where to get it:
- Same location as REST URL in Upstash Console
- Copy the “REST Token” (starts with
AX...)
Authentication
Secret key used by NextAuth.js for JWT signing and encryptionRequirements:Example:
- Minimum 32 characters
- Cryptographically random
- Must be different between environments
Optional Variables
The base URL of your application (used to construct short links)Used in: Production:
app/lib/links.ts:63 - Generates the full short URL when creating linksLocal development:This variable is prefixed with
NEXT_PUBLIC_ so it’s available in the browser. The short URLs displayed to users will use this base URL.The canonical URL of your site (used for callbacks)Local development:Production:
Most platforms (Vercel, Netlify) auto-detect this. Only set manually if you experience redirect issues.
Node.js environment modeValues:
development- Local development with hot reloadproduction- Optimized production buildtest- Testing environment
Next.js automatically sets this during
npm run dev (development) and npm run build (production)Environment File Examples
Platform-Specific Setup
- Vercel
- Railway
- Docker
Add environment variables in the Vercel dashboard:
Add Variables
Add each variable with appropriate scope:
- Production - Only production deployments
- Preview - Pull request previews
- Development - Local development with
vercel dev
Vercel automatically sets
NEXTAUTH_URL based on your deployment URLSecurity Best Practices
Never Commit Secrets
Add
.env* to .gitignore to prevent accidentally committing secrets.gitignore
Use Strong Secrets
Generate cryptographically secure random strings:
Rotate Regularly
Change secrets periodically, especially:
- After team member departures
- If secrets may have been exposed
- Every 90 days for compliance
Separate Environments
Use different credentials for:
- Development
- Staging
- Production
Validation
The application validates environment variables at startup. Missing required variables will cause errors:app/lib/db.ts
Testing Configuration
Verify your environment variables are loaded:- Variable names are spelled correctly
- Values have no extra quotes or spaces
.env.localis in the project root- You’ve restarted the dev server after changes
Troubleshooting
Variables not loading
Variables not loading
Symptoms: App can’t connect to database/RedisSolutions:
- Verify
.env.localis in project root (same level aspackage.json) - Restart the dev server - env vars are loaded at startup
- Check for typos in variable names
- Ensure no quotes around values in
.envfiles
NextAuth callback errors
NextAuth callback errors
Symptoms: Redirect loops, callback URL mismatchSolutions:
- Set
NEXTAUTH_URLto match your domain exactly - Include protocol:
https://not justshrtnr.com - Verify
trustHost: trueinauth.tsfor production - Check that
AUTH_SECRETis set and at least 32 characters
Database SSL errors
Database SSL errors
Symptoms:
SSL SYSCALL error or connection refusedSolutions:- Add
?sslmode=requiretoPOSTGRES_URL - Use the pooled connection string from Neon for serverless
- Verify your Neon project is not suspended (free tier limits)
Redis authentication failed
Redis authentication failed
Symptoms:
401 Unauthorized from UpstashSolutions:- Verify
UPSTASH_REDIS_REST_TOKENis the REST token (not the Redis password) - Check that URL and token are from the same database
- Ensure no trailing spaces in
.envfile - Regenerate token in Upstash dashboard if needed
Next Steps
Database Setup
Initialize the PostgreSQL schema and run migrations
Deployment Guide
Deploy your application to production