Lesson 43 of 158 – Postman Environment
43%

Postman Environment

A Postman Environment is a collection of variables that allows you to store reusable values such as API URLs, ports, tokens, and other configuration data.

Note: Environments are especially useful when the same API is available on different servers, such as localhost, testing, and production servers.

1. What is a Postman Environment?

A Postman Environment stores variables that can be reused in API requests.

For example:

base_url
api_token
user_id

Instead of writing the same values in every request, we can use environment variables.

2. Why Use an Environment?

An environment makes API testing easier when values change between different environments.

For example:

  • Local development server
  • Testing server
  • Production server

The API requests can remain the same while the variable values change.

3. Environment Variables

An environment contains variables and their values.

Example:

base_url = http://localhost/api

The variable name is base_url and its value is http://localhost/api.

4. Create an Environment

You can create an environment from the Environments section in Postman.

  1. Open Postman.
  2. Open the Environments section.
  3. Create a new environment.
  4. Enter an environment name.
  5. Add variables.
  6. Save the environment.

5. Environment Name

Give the environment a meaningful name.

Examples:

  • Local
  • Development
  • Testing
  • Production

A clear name helps identify which configuration is currently being used.

6. Local Environment

During development, an API may run on a local computer.

Example:

http://localhost/api

This URL can be stored in an environment variable.

7. Development Environment

A development environment can contain values used by a development server.

Example:

base_url = https://dev.example.com/api

API requests can use this value instead of hard-coding the URL.

8. Production Environment

A production environment contains values used by the live application.

Example:

base_url = https://example.com/api

The same API requests can use this environment without changing every URL manually.

9. Using a Variable in a URL

Postman environment variables can be referenced using double curly braces.

{{base_url}}/students

If base_url contains:

http://localhost/api

the resulting URL becomes:

http://localhost/api/students

10. Double Curly Braces

Postman uses double curly braces to reference variables.

{{variable_name}}

Example:

{{base_url}}

This tells Postman to use the value stored in the variable.

11. Base URL Variable

A base URL is one of the most useful environment variables.

base_url = http://localhost/api

Then requests can use:

{{base_url}}/students.php
{{base_url}}/users.php
{{base_url}}/courses.php

12. API URL Example

Suppose the API endpoint is:

http://localhost/api/students.php

Instead of writing the complete URL repeatedly, create:

base_url = http://localhost/api

Then use:

{{base_url}}/students.php

13. Multiple Environment Variables

An environment can contain multiple variables.

base_url
api_version
user_id
token

Each variable can store a different value.

14. API Version Variable

You can store the API version in a variable.

api_version = v1

Then a URL can be written as:

{{base_url}}/{{api_version}}/students

This can make version changes easier to manage.

15. User ID Variable

A user ID can also be stored in an environment variable.

user_id = 25

It can then be used in a request such as:

{{base_url}}/users/{{user_id}}

16. Token Variable

Authentication tokens can be represented by environment variables when testing protected APIs.

token = your_token_here

A request header could then use:

Authorization: Bearer {{token}}

The actual token value is taken from the selected environment.

17. Select an Environment

When multiple environments exist, select the environment you want to use for the current API testing session.

For example:

Local
Development
Production

The selected environment determines which variable values Postman uses.

18. Switching Between Environments

Suppose your API has two servers.

Local:
http://localhost/api

Production:
https://example.com/api

You can switch environments instead of changing every API request manually.

19. Environment with Collections

Collections and environments work together.

Collection
     ↓
{{base_url}}/students
     ↓
Selected Environment
     ↓
Actual API URL

This allows the same collection to work with different API servers.

20. Development and Production Example

Development:

base_url = https://dev.example.com/api

Production:

base_url = https://example.com/api

The request can remain:

{{base_url}}/students

Only the selected environment changes.

21. Environment for React Native API Testing

When developing a React Native application, the backend API may change between development and production.

Postman environments help test the different API URLs before connecting the mobile application to them.

React Native App
       ↓
REST API
       ↓
Postman Testing

22. Environment Variables for Headers

Variables can also be used inside headers.

Example:

Authorization: Bearer {{token}}

Postman replaces the variable with its configured value when the request is sent.

23. Environment Variables for Query Parameters

Variables can also be used in query parameters.

{{base_url}}/students?course={{course}}

For example, if:

course = ADCA

the request becomes:

{{base_url}}/students?course=ADCA

24. Environment Variables for Request Data

Variables can also be useful inside request data.

{
    "name": "{{student_name}}",
    "course": "{{course}}"
}

This allows test data to be changed without rewriting the entire request.

25. Common Environment Mistakes

  • Using the wrong environment
  • Misspelling variable names
  • Forgetting double curly braces
  • Using an incorrect base URL
  • Forgetting to update variable values
  • Using the wrong token

Always check the selected environment when an API request does not work as expected.

26. Good Environment Naming

Use clear environment names such as:

Student API - Local
Student API - Development
Student API - Production

Clear names reduce confusion when several environments are available.

27. Environment Testing Checklist

Before sending an API request, check:

  1. The correct environment is selected.
  2. The base URL is correct.
  3. The endpoint is correct.
  4. Required variables have values.
  5. Authentication variables are correct when required.
  6. The request method is correct.

28. Complete Environment Example

Environment: Student API - Local

base_url = http://localhost/api
api_version = v1
user_id = 10
course = ADCA
token = your_token_here

A request can use these variables like this:

{{base_url}}/{{api_version}}/students/{{user_id}}

29. Environment Workflow

Create Environment
       ↓
Add Variables
       ↓
Select Environment
       ↓
Create Collection
       ↓
Use {{variables}}
       ↓
Send API Request
       ↓
Check Response

This workflow makes API testing more organized and reusable.

30. Postman Environment Summary

Postman Environments allow developers to store reusable configuration values. They are particularly useful when working with multiple API servers or changing values such as base URLs, user IDs, courses, and authentication tokens.

Environment
     ↓
Variables
     ↓
{{variable}}
     ↓
API Request
     ↓
REST API

📌 Key Points

  • A Postman Environment stores reusable variables.
  • Variables can store API URLs and other configuration values.
  • Variables are referenced using double curly braces.
  • Example: {{base_url}}.
  • Environments are useful for local, development, and production APIs.
  • The same collection can work with different environments.
  • Tokens can be represented using environment variables.
  • Variables can be used in URLs, headers, query parameters, and request data.
  • Select the correct environment before sending requests.
  • Environment variables make API testing more organized and reusable.

🧠 Quick Quiz

Question: How is a Postman environment variable usually referenced inside an API request?