rest_to_postman_env
Synchronize REST application environment variables with Postman by creating or updating Postman environments, automatically marking sensitive variables as secrets.
Instructions
Creates or updates a Postman environment with the provided environment variables. This tool helps synchronize your REST application's environment configuration with Postman. It supports both creating new environments and updating existing ones in your Postman workspace. Environment variables related to sensitive data (containing 'token' in their names) are automatically marked as secrets. Here's an example:
{ "envName": "REST Environment", "envVars": { "API_URL": "https://api.example.com", "API_TOKEN": "secret-token-1" } }
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| envName | Yes | The name of the Postman environment to create or update | |
| envVars | Yes | A record of environment variables to be added to the Postman environment. Format: { [key: string]: string } |
Implementation Reference
- src/postman.ts:42-110 (handler)Core handler function that implements the tool logic: transforms input envVars into Postman EnvValue format (auto-detects secrets), checks for existing environment, and POST/PUT to Postman API to create/update.export async function pushEnvironment(environmentName: string, envVars: Record<string, string>, postmanApiKey?: string, workspaceId?: string): Promise<void> { try { const apiKey = postmanApiKey || process.env.POSTMAN_API_KEY; const activeWorkspaceId = workspaceId || process.env.POSTMAN_ACTIVE_WORKSPACE_ID; // Read all environment variables except POSTMAN_API_KEY and POSTMAN_ACTIVE_WORKSPACE_ID const envValues: EnvValue[] = []; for (const [key, value] of Object.entries(envVars)) { envValues.push({ key, value, enabled: true, type: key.includes('token') ? 'secret' : 'default' }); } // Check if environment already exists const workspaceResponse = await axios({ method: 'get', url: `https://api.getpostman.com/workspaces/${activeWorkspaceId}`, headers: { 'X-Api-Key': apiKey } }); const workspaceData = workspaceResponse.data as PostmanWorkspaceResponse; const existingEnvironment = workspaceData.workspace.environments?.find( env => env.name === environmentName ); // Prepare the environment data const environmentData = { environment: { name: environmentName, values: envValues } }; let response; if (existingEnvironment) { response = await axios({ method: 'put', url: `https://api.getpostman.com/environments/${existingEnvironment.id}`, headers: { 'X-Api-Key': apiKey, 'Content-Type': 'application/json' }, data: environmentData }); } else { response = await axios({ method: 'post', url: 'https://api.getpostman.com/environments', headers: { 'X-Api-Key': apiKey, 'Content-Type': 'application/json' }, data: { ...environmentData, workspace: activeWorkspaceId } }); } } catch (error) { throw error; } }
- src/mcp.ts:82-102 (registration)Tool registration in the tools list, including name, description, and JSON input schema for MCP listTools.{ "name": "rest_to_postman_env", "description": "Creates or updates a Postman environment with the provided environment variables. This tool helps synchronize your REST application's environment configuration with Postman. It supports both creating new environments and updating existing ones in your Postman workspace. Environment variables related to sensitive data (containing 'token' in their names) are automatically marked as secrets. Here's an example: \n\n" + envExample, "inputSchema": { "type": "object", "properties": { "envName": { "type": "string", "description": "The name of the Postman environment to create or update" }, "envVars": { "type": "object", "description": "A record of environment variables to be added to the Postman environment. Format: { [key: string]: string }", "additionalProperties": { "type": "string" } } }, "required": ["envName", "envVars"] } },
- src/mcp.ts:390-401 (handler)MCP callTool request handler specific branch that extracts arguments and invokes the pushEnvironment implementation.if (request.params.name === "rest_to_postman_env") { if (!request.params.arguments) { throw new McpError(ErrorCode.InvalidParams, "Missing arguments"); } const { envName, envVars } = request.params.arguments as { envName: string; envVars: Record<string, string> }; await pushEnvironment(envName, envVars, process.env.POSTMAN_API_KEY, process.env.POSTMAN_ACTIVE_WORKSPACE_ID); return { content: [{ type: "text", text: `Successfully created/updated Postman environment: ${envName}` }] };
- src/types/index.ts:55-60 (schema)TypeScript interface defining the structure of environment variables used by pushEnvironment.export interface EnvValue { key: string; value: string; enabled: boolean; type: string; }