Created
March 10, 2026 09:52
-
-
Save DannyDainton/942ee1a270c0c3459611d354ae7bfaaf to your computer and use it in GitHub Desktop.
This script can be using to move a Postman Environment from a Shared Workspace to an Internal Workspace.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Copy a Postman Environment to a different workspace | |
| # Create a new Postman API Key for your user https://go.postman.co/settings/me/api-keys and add this to the API_KEY variable | |
| # Usage: ./copy_environment.sh <environment_id> <workspace_id> | |
| set -euo pipefail | |
| API_KEY="POSTMAN_API_KEY" | |
| BASE_URL="https://api.getpostman.com" | |
| ENVIRONMENT_ID="${1:?Usage: $0 <environment_id> <workspace_id>}" | |
| WORKSPACE_ID="${2:?Usage: $0 <environment_id> <workspace_id>}" | |
| echo "Fetching environment ${ENVIRONMENT_ID}..." | |
| RESPONSE=$(curl -s -w "\n%{http_code}" \ | |
| -X GET "${BASE_URL}/environments/${ENVIRONMENT_ID}" \ | |
| -H "X-Api-Key: ${API_KEY}") | |
| HTTP_CODE=$(echo "$RESPONSE" | tail -n1) | |
| BODY=$(echo "$RESPONSE" | sed '$d') | |
| if [[ "$HTTP_CODE" -ne 200 ]]; then | |
| echo "Error: GET /environments/${ENVIRONMENT_ID} returned HTTP ${HTTP_CODE}" | |
| echo "$BODY" | |
| exit 1 | |
| fi | |
| echo "Creating environment in workspace ${WORKSPACE_ID}..." | |
| POST_RESPONSE=$(curl -s -w "\n%{http_code}" \ | |
| -X POST "${BASE_URL}/environments?workspace=${WORKSPACE_ID}" \ | |
| -H "X-Api-Key: ${API_KEY}" \ | |
| -H "Content-Type: application/json" \ | |
| -d "$BODY") | |
| POST_HTTP_CODE=$(echo "$POST_RESPONSE" | tail -n1) | |
| POST_BODY=$(echo "$POST_RESPONSE" | sed '$d') | |
| if [[ "$POST_HTTP_CODE" -ne 200 ]]; then | |
| echo "Error: POST /environments returned HTTP ${POST_HTTP_CODE}" | |
| echo "$POST_BODY" | |
| exit 1 | |
| fi | |
| echo "Environment copied successfully!" | |
| echo "$POST_BODY" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment