Skip to content

Instantly share code, notes, and snippets.

@webowodev
Created October 23, 2025 01:35
Show Gist options
  • Save webowodev/a87ef75ccb493bf09262751b33dc6b38 to your computer and use it in GitHub Desktop.
Save webowodev/a87ef75ccb493bf09262751b33dc6b38 to your computer and use it in GitHub Desktop.
Generate .env file from Vault API
if [ -z "$TOKEN" ]; then
echo "Token not provided!"
exit 1
else
if [ -z "$API_URL" ]; then
echo "Missing the API url, I don't know where the env is :/"
exit 2
else
echo "Getting environment variable from $API_URL"
if [ -z "$OUTPUT" ]; then
# set default output name
OUTPUT=.env
fi
# Execute curl and jq commands and store the result in a temporary file
if ! curl --fail -X 'GET' \
"$API_URL" \
-H 'accept: */*' \
-H "X-Vault-Token: $TOKEN" > temp.json; then
echo "Failed to fetch environment variables"
rm -f temp.json
exit 1
fi
if ! jq -r '.data.data | to_entries[] | if .value | type == "string" and contains("\n") then "\(.key)=\"\(.value|tostring|gsub("\\n"; "\\n"))\"" else "\(.key)=\(.value)" end' temp.json > "$OUTPUT"; then
echo "Failed to process environment variables"
rm -f temp.json
exit 1
fi
rm -f temp.json
fi
fi
@webowodev
Copy link
Author

webowodev commented Oct 23, 2025

Prerequisite

Usage

API_URL=https://yourvaultapiurl.com/v1/{KV_PATH}/data/{YOUR_SECRET_PATH} TOKEN=hvs..... OUTPUT=.env sh generate-env.sh
Variable Description Mandatory
API_URL Your vault server url with the KV secret path Yes
TOKEN Your vault token Yes
OUTPUT Your desired .env output file. Default .env No

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment