ENV FILE SETUP

NestJS Backend API for GoGrab Space platform - Architecture, Best Practices, and Common Issues

🔧 Backend API - Environment Setup (REQUIRED!)

Date: November 18, 2025
Issue: Backend was failing with MongoDB errors because .env file was missing!


✅ FIXED - .env File Created

I've created a .env file at apis/backend-api/.env with the following configuration:

# MongoDB Connection (REQUIRED)
MONGODB_CONNECTION_STRING=mongodb+srv://username:[email protected]/?retryWrites=true&w=majority
MONGODB_DATABASE=gograb-dev

# JWT Secret (REQUIRED)
JWT_SECRET=your-super-secret-jwt-key-change-this-in-production

# Server Port
PORT=3000

# Node Environment
NODE_ENV=development

# CORS Origins
ALLOWED_ORIGINS=http://localhost:5200,http://localhost:5701,http://localhost:4200,http://localhost:5300

🚨 IMPORTANT - You MUST Update These Values:

1. MongoDB Connection String (REQUIRED):

Get your MongoDB Atlas connection string:

  1. Go to MongoDB Atlas
  2. Click "Connect" on your cluster
  3. Select "Connect your application"
  4. Copy the connection string
  5. Replace username and password with your actual credentials

Example:

MONGODB_CONNECTION_STRING=mongodb+srv://paco:[email protected]/?retryWrites=true&w=majority

OR use local MongoDB:

MONGODB_CONNECTION_STRING=mongodb://localhost:27017/gograb-dev

2. JWT Secret (REQUIRED):

Generate a secure random string:

# Generate a secure JWT secret
node -e "console.log(require('crypto').randomBytes(64).toString('hex'))"

Then update the .env file:

JWT_SECRET=a8f5f167f44f4964e6c998dee827110c03b8c2e9d3a37f78b4a6f7e8c2d9e1f0

🔄 After Updating .env:

  1. Restart the backend:

    # Press Ctrl+C in the terminal running the backend
    yarn serve:backend-api
    
  2. Check the logs - You should see:

    🚀 ═══════════════════════════════════════════════════════
       GoGrab Backend API - READY
       ═══════════════════════════════════════════════════════
    
    📡 Server:     http://localhost:3000/api
    🗄️  Database:   gograb-dev
    🌍 Environment: development
    
    🔒 CORS Configuration:
       Total Origins: 4
       1. http://localhost:5200
       2. http://localhost:5701
       3. http://localhost:4200
       4. http://localhost:5300
    
  3. Test it - Try saving something in the frontend


🐛 Why This Was Causing Errors:

  1. No .env file → No MongoDB connection string
  2. Backend couldn't connect to MongoDB → All write operations failed
  3. Frontend showed 500 errors → "Failed to save universe data"

Now that we have a .env file, the backend can connect to MongoDB! ✅


📚 Environment Variables Explained:

Variable Purpose Required?
MONGODB_CONNECTION_STRING MongoDB Atlas or local connection ✅ YES
MONGODB_DATABASE Database name ✅ YES
JWT_SECRET Secret for JWT token signing ✅ YES
PORT Backend server port No (defaults to 3000)
NODE_ENV Environment mode No (defaults to development)
ALLOWED_ORIGINS CORS allowed origins No (has defaults)
OPENAI_API_KEY OpenAI API access No (only if using AI)
ANTHROPIC_API_KEY Anthropic API access No (only if using AI)

🔒 Security Notes:

  1. ✅ .env is gitignored - Your secrets won't be committed
  2. ⚠️ Never commit real credentials to Git
  3. ⚠️ Use different secrets for production
  4. ✅ Railway has its own environment variables - This is just for local

🎯 Quick Setup Checklist:


Status: ✅ .env file created - Now update the MongoDB connection string!