Overview
The Cat Data API uses environment variables to configure database connections, authentication, and server settings. These variables are loaded from a.env file in the project root using the dotenv package.
Required Variables
Create a.env file in the root directory with the following configuration:
.env
Database Variables
PostgreSQL connection settings used by Knex inknexfile.js:
PGHOST
- Description: PostgreSQL server hostname
- Default:
localhost - Example:
localhostordb.example.com
knexfile.js:7
PGPORT
- Description: PostgreSQL server port
- Default:
5432 - Example:
5432
knexfile.js:8
PGUSER
- Description: PostgreSQL username
- Required: Yes
- Example:
postgresor your database user
knexfile.js:9
PGPASSWORD
- Description: PostgreSQL password
- Required: Yes
- Security: Keep this secure and never expose it in logs or error messages
knexfile.js:10
PGDATABASE
- Description: PostgreSQL database name
- Default:
cat_data - Example:
cat_dataorcat_data_production
knexfile.js:11
Authentication Variables
Auth0 JWT authentication configuration used insrc/middlewares/checkJwt.ts:
AUTH0_DOMAIN
- Description: Your Auth0 tenant domain (must include protocol and trailing slash)
- Required: Yes
- Format:
https://your-domain.auth0.com/orhttps://your-domain.us.auth0.com/ - Usage: Used to fetch JWKS keys and validate JWT issuer
src/middlewares/checkJwt.ts:7-8
src/middlewares/checkJwt.ts:15
The
AUTH0_DOMAIN must include the protocol (https://) and end with a trailing slash (/) to correctly build the JWKS endpoint URL.AUTH0_AUDIENCE
- Description: Auth0 API audience identifier
- Required: Yes
- Format: API identifier from your Auth0 dashboard (e.g.,
https://api.example.com) - Usage: Validates that JWT tokens are intended for this API
src/middlewares/checkJwt.ts:17
Server Variables
PORT
- Description: Port number for the Express server
- Default:
3000 - Example:
3000,8080, or5000
src/index.ts:15
src/index.ts:86-88
NODE_ENV
- Description: Application environment
- Default:
development - Options:
development,production,test - Usage: Determines which Knex configuration to use
src/db.ts:4
Set
NODE_ENV=production when deploying to production to use optimized settings and production database configuration.Usage in Code
Environment variables are loaded at the start of the application:src/index.ts:8,12
dotenv.config() call reads the .env file and makes variables available via process.env.
Environment-Specific Configurations
Security Best Practices
Protecting Sensitive Data
-
Add
.envto.gitignore:.gitignore -
Use secret management in production:
- AWS Secrets Manager
- HashiCorp Vault
- Docker secrets
- Kubernetes secrets
-
Provide an example file:
Create
.env.examplewith placeholder values:.env.example
Troubleshooting
Variables not loading
Ensuredotenv.config() is called before accessing environment variables:
Auth0 connection errors
Verify yourAUTH0_DOMAIN format:
- ✅ Correct:
https://example.auth0.com/ - ❌ Wrong:
example.auth0.com(missing protocol and slash) - ❌ Wrong:
https://example.auth0.com(missing trailing slash)