Skip to main content

Environment Variables

The Sistema de Gestión de Propiedades API uses environment variables to configure authentication credentials and other settings.

Required Variables

The API requires these environment variables:
VariablePurposeExample
ADMIN_USERAdministrator username for Basic Authadmin
ADMIN_PASSAdministrator password for Basic Authsecretpassword123
DBD1 database binding (auto-configured)-

Authentication Variables

ADMIN_USER

The username for HTTP Basic Authentication on protected endpoints. Used by: Backend/src/middlewares/auth.ts:7
export const adminAuthMiddleware = async (c: Context, next: Next) => {
  const auth = basicAuth({
    username: c.env.ADMIN_USER,  // <-- Environment variable
    password: c.env.ADMIN_PASS,
  })
  
  return auth(c, next)
}
Protected endpoints:
  • POST /api/propiedades - Create property
  • PATCH /api/propiedades/{id} - Update property
  • DELETE /api/propiedades/{id} - Delete property

ADMIN_PASS

The password for HTTP Basic Authentication. Security notes:
  • Never commit this to version control
  • Use a strong, unique password
  • Rotate regularly
  • Store as a Wrangler secret (encrypted)

Setting Variables

Local Development

For local development with wrangler dev, create a .dev.vars file:
# .dev.vars
ADMIN_USER=admin
ADMIN_PASS=dev-password-123
Add .dev.vars to your .gitignore to prevent committing credentials.
.gitignore:
.dev.vars
wrangler.toml

Production (Wrangler Secrets)

For production, use Wrangler secrets (encrypted and never visible):
1

Set ADMIN_USER secret

wrangler secret put ADMIN_USER
When prompted, enter your admin username:
Enter a secret value: admin
2

Set ADMIN_PASS secret

wrangler secret put ADMIN_PASS
When prompted, enter a strong password:
Enter a secret value: your-secure-password-here
3

Verify secrets are set

wrangler secret list
Output:
[
  {
    "name": "ADMIN_USER",
    "type": "secret_text"
  },
  {
    "name": "ADMIN_PASS",
    "type": "secret_text"
  }
]
Secrets are encrypted and never displayed. You can only overwrite or delete them.
You can set variables in wrangler.toml, but only use this for non-sensitive values:
# wrangler.toml
[vars]
ADMIN_USER = "admin"  # OK for username
# NEVER put passwords here
Never store passwords in wrangler.toml. Always use secrets for sensitive data.

Accessing Variables in Code

Variables are accessed through the context’s env object:
import { Context } from 'hono';

export const someHandler = async (c: Context) => {
  // Access environment variables
  const username = c.env.ADMIN_USER;
  const password = c.env.ADMIN_PASS;
  
  // Access D1 database binding
  const db = c.env.DB;
  
  // Use them
  const result = await db.prepare('SELECT * FROM propiedades').all();
};

D1 Database Binding

The DB binding is configured in wrangler.toml:
[[d1_databases]]
binding = "DB"  # <-- This becomes c.env.DB
database_name = "propiedades-db"
database_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
This binding gives your Worker access to the D1 database:
// Access the database
const { results } = await c.env.DB.prepare(
  'SELECT * FROM propiedades'
).all();

Environment-Specific Configuration

Multiple Environments

Configure separate environments for staging and production:
# wrangler.toml

# Staging environment
[env.staging]
name = "inmobiliaria-api-staging"

[[env.staging.d1_databases]]
binding = "DB"
database_name = "propiedades-staging"
database_id = "staging-db-id"

# Production environment
[env.production]
name = "inmobiliaria-api-production"

[[env.production.d1_databases]]
binding = "DB"
database_name = "propiedades-production"
database_id = "production-db-id"

Set Secrets per Environment

# Staging secrets
wrangler secret put ADMIN_USER --env staging
wrangler secret put ADMIN_PASS --env staging

# Production secrets
wrangler secret put ADMIN_USER --env production
wrangler secret put ADMIN_PASS --env production

Deploy to Specific Environment

# Deploy to staging
wrangler deploy --env staging

# Deploy to production
wrangler deploy --env production

Managing Secrets

List Secrets

wrangler secret list

Update a Secret

wrangler secret put ADMIN_PASS
# Enter new password

Delete a Secret

wrangler secret delete ADMIN_PASS
Deleting a secret will cause authentication to fail until you set a new one.

Security Best Practices

1

Use strong passwords

Generate secure passwords:
# On Linux/Mac
openssl rand -base64 32

# Example output
K7gNU3sdo+OL0wNhqoVWhr3g6s1xYv72ol/pe/Unols=
2

Rotate credentials regularly

Update secrets periodically:
wrangler secret put ADMIN_PASS
3

Use different credentials per environment

Don’t reuse staging credentials in production.
4

Never commit credentials

Add to .gitignore:
.dev.vars
wrangler.toml  # If it contains secrets
5

Limit access to secrets

Only trusted team members should have access to production secrets.

Verifying Configuration

Test Locally

1

Start dev server

wrangler dev
2

Test authentication

curl -u admin:dev-password-123 http://localhost:8787/api/auth/verify
Should return:
{
  "authenticated": true,
  "user": "admin"
}

Test Production

1

Deploy

wrangler deploy
2

Test authentication

curl -u admin:your-production-password \
  https://your-worker.workers.dev/api/auth/verify

TypeScript Types

Generate TypeScript types for your environment bindings:
npm run cf-typegen
This creates type definitions based on your wrangler.toml:
// Generated types
interface CloudflareBindings {
  ADMIN_USER: string;
  ADMIN_PASS: string;
  DB: D1Database;
}

Troubleshooting

Cause: Secrets not set or incorrectSolution:
# Verify secrets are set
wrangler secret list

# If missing, add them
wrangler secret put ADMIN_USER
wrangler secret put ADMIN_PASS
Cause: D1 binding not configuredSolution: Check wrangler.toml:
[[d1_databases]]
binding = "DB"
database_name = "propiedades-db"
database_id = "your-database-id"
Cause: Secrets not set in productionSolution: .dev.vars only works locally. Set secrets for production:
wrangler secret put ADMIN_USER
wrangler secret put ADMIN_PASS
This is normal. Secrets are encrypted and never displayed. You can only overwrite them:
wrangler secret put ADMIN_PASS

Environment Variable Checklist

Before deploying:
  • .dev.vars created for local development
  • .dev.vars added to .gitignore
  • Production secrets set with wrangler secret put
  • D1 database binding configured in wrangler.toml
  • Authentication tested locally with wrangler dev
  • Authentication tested in production after deployment
  • Strong passwords used for production
  • Credentials documented securely (password manager)

Next Steps

Cloudflare Workers

Complete deployment guide

Database Setup

Configure the D1 database

Authentication

Learn about the auth system

Quickstart

Start using the deployed API

Build docs developers (and LLMs) love