BASH
Dynamic API Endpoint Switching
Configure dynamic API endpoints for development, staging, or production environments using a simple bash script that exports environment variables.
#!/bin/bash
ENV=$1
case "$ENV" in
"dev")
export API_BASE_URL="http://localhost:3000/api/v1"
export ANALYTICS_KEY="dev_analytics_123"
echo "Development environment variables set."
;;
"staging")
export API_BASE_URL="https://staging.example.com/api/v1"
export ANALYTICS_KEY="staging_analytics_456"
echo "Staging environment variables set."
;;
"prod")
export API_BASE_URL="https://api.example.com/api/v1"
export ANALYTICS_KEY="prod_analytics_789"
echo "Production environment variables set."
;;
*)
echo "Usage: source $0 [dev|staging|prod]"
echo "No environment specified. Using default (local development)."
export API_BASE_URL="http://localhost:8080/api/v1"
export ANALYTICS_KEY="local_analytics_000"
;;
esac
echo "API_BASE_URL: $API_BASE_URL"
echo "ANALYTICS_KEY: $ANALYTICS_KEY"
# Example of how to use this in your shell:
# source ./set_env.sh dev
# node -e 'console.log(process.env.API_BASE_URL)'
How it works: This script provides a mechanism to dynamically set environment variables based on a specified environment (e.g., `dev`, `staging`, `prod`). When sourced (`source ./script.sh dev`), it exports `API_BASE_URL` and `ANALYTICS_KEY` into the current shell session, allowing applications like Node.js or frontend builds to consume the correct API endpoints or configuration. This is crucial for managing configurations across different deployment stages.