Page 1 of 10
Table of Contents
1. Set Up Your Environment
2. Define Your API Endpoints
3. Handle Tokens and Authentication
4. Run and Automate Your Tests
5. Debug and Verify
1. Environment Variables for Your Node Server
2. Environment (Variables) in Postman
Example of a Postman Environment
3. Typical Workflow
4. Conclusion
1. Identify Your Auth/Login Endpoint
2. Use the Token for Discount Coupons Requests
3. Verify and Automate
Tips
how can i use postman to run my project and get the api
endpoints and tokens
Page 2 of 10
Here's a step-by-step guide on how to use Postman to run your project, test API endpoints, and
handle tokens:
1. Set Up Your Environment
Install Postman: Download and install Postman if you haven't already.
Create an Environment: In Postman, create an environment (e.g., “Local API”) where you can
define variables like your API's base URL and any tokens you may need.
2. Define Your API Endpoints
Create a Collection: Organize your API endpoints by creating a new collection. This helps keep
your endpoints and tests in one place.
Add Requests: For each API endpoint you want to test, add a new request in your collection.
Specify the HTTP method (GET, POST, etc.) and the endpoint URL (using the base URL variable if
applicable, e.g., {{baseUrl}}/login ).
3. Handle Tokens and Authentication
Login Request (or Token Generation):
Create a request to the endpoint that returns your token (often a login or auth endpoint).
In the request body (or headers), provide the necessary credentials.
Extract the Token Automatically:
Go to the Tests tab of the login request and add a script to extract the token from the
response. For example:
javascript
let responseData = pm.response.json();
if (responseData.token) {
pm.environment.set("token", responseData.token);
}
This script stores the token in your environment variable ( token ), so you can reuse it in
subsequent requests.
Use the Token in Other Requests:
For any protected endpoint, add an Authorization header with the value Bearer {{token}} .
This tells Postman to substitute {{token}} with the value stored in your environment.
4. Run and Automate Your Tests
Send Individual Requests:
Page 3 of 10
You can manually send each request by clicking the Send button to ensure everything
works as expected.
Use the Collection Runner:
If you have multiple endpoints to test, use the Collection Runner to run your collection
sequentially. This is especially useful for workflows where one request's output (like a
token) is needed for the next request.
Automate with Newman:
For continuous integration or automated testing, you can run your Postman collections
using Newman (Postman's CLI tool).
5. Debug and Verify
Check Response Data:
Use the Postman console (accessible via View > Show Postman Console) to inspect request
and response details if things aren't working as expected.
Update Environment Variables:
As you run tests, ensure that your environment variables (like baseUrl and token ) are
correctly set and updated.
By following these steps, you can efficiently use Postman to run your project, explore your API
endpoints, and manage tokens automatically. This setup not only helps with manual testing but
also sets a strong foundation for automated API testing.
should i import to postman ?
Page 4 of 10
You typically don't import your entire codebase or .env file directly into Postman. Instead, you run
your server locally (using those environment variables in Node) and then create or import API
requests in Postman to test your endpoints. Here's how you might handle it:
1. Start Your Server
Make sure your server is running locally with the environment variables from your .env file.
For example, you might run:
bash
npm run dev
This will start your Express app on a specific port (e.g., http://localhost:3000 ).
2. Create a Postman Environment (Optional but Recommended)
In Postman, go to Environments and create one (e.g., “Local Dev”).
Define a variable like baseUrl set to http://localhost:3000 (or whatever port your app is
using).
3. Add Requests in Postman
In a Postman Collection, add a new request.
For the URL, use {{baseUrl}}/your-endpoint if you created a baseUrl variable. Otherwise, type
in http://localhost:3000/your-endpoint .
If your routes require a JWT, you can store that token in your Postman environment as well.
For example:
json
{
"key": "jwt",
"value": "yourTokenHere",
"enabled": true
}
Then, in your request headers, set Authorization to Bearer {{jwt}} .
4. (Optional) Import an Existing Collection or OpenAPI Spec
If you have a Postman collection file (JSON) or an OpenAPI (Swagger) spec, you can import
that into Postman to auto-generate the endpoints.
If you don't have a collection or spec, you'll need to create the requests manually in
Postman.
5. Send Requests & Test
Click Send on each request to test.
Page 5 of 10
Check the response body, headers, and status code in Postman to ensure everything's
working.
6. Use Scripts for Tokens (If Needed)
If your login endpoint returns a token, you can capture it automatically in Postman using a
Test script:
javascript
let jsonData = pm.response.json();
pm.environment.set("jwt", jsonData.token);
Then subsequent requests can use {{jwt}} in the Authorization header.
Key Takeaway: You don't import the .env file itself into Postman. The .env is for your Node server.
In Postman, you'll create requests pointing to the running server and optionally set up an
environment (with variables like baseUrl or jwt ) to make testing easier.
1/2
It looks like you're showing the environment variables from your .env file (or a similar environment
setup) in the screenshot. Keep in mind that these variables are primarily for your Node/Express
server, not for Postman. Here's what that means in practice:
Page 6 of 10
1. Environment Variables for Your Node Server
In a Node.js project, the .env file typically contains things like:
PORT: The port your server will run on.
DB_URL: The MongoDB connection string.
JWT_SECRET: A secret key used for signing JSON Web Tokens.
JWT_EXPIRES_IN: The token's expiration time.
When you run your server (e.g., npm run dev ), your server reads from this .env file using packages
like dotenv . This makes those values available in your code, so you can do something like:
js
require("dotenv").config();
const PORT = process.env.PORT;
const DB_URL = process.env.DB_URL;
const JWT_SECRET = process.env.JWT_SECRET;
const JWT_EXPIRES_IN = process.env.JWT_EXPIRES_IN;
2. Environment (Variables) in Postman
Postman also allows you to create “environments,” but these are completely separate from your
Node .env file. Postman environments are used to store request-specific variables, such as:
baseUrl for the server (e.g., http://localhost:3000 ).
token if you need a JWT for authorization in your requests.
Example of a Postman Environment
Variable Initial Value Current Value
baseUrl http://localhost:3000 http://localhost:3000
token (empty) (empty)
You would then use {{baseUrl}}/api/whatever in your Postman requests.
Page 7 of 10
Once you log in and get a JWT, you can store that JWT in token and then reference {{token}} in
your request headers.
3. Typical Workflow
1. Run your Node server with your .env variables in place:
bash
npm run dev
Your server will now be listening at something like http://localhost:3000 .
2. Set up Postman so it knows where to send requests:
Create (or edit) a Postman environment with a variable like baseUrl = http://localhost:3000 .
For each endpoint, use {{baseUrl}}/route .
3. (If you need a token):
Create a “Login” request in Postman to your /login endpoint.
Provide the credentials in the body or headers (whatever your API requires).
In the Tests tab of that request, add a script to save the token to your Postman
environment:
js
let jsonData = pm.response.json();
pm.environment.set("token", jsonData.token);
Now, in subsequent requests, you can reference that token in the Authorization header:
css
Authorization: Bearer {{token}}
4. Don't store server secrets in Postman (like DB_URL or JWT_SECRET )—those stay on the server
side. Postman doesn't need to know your database connection string or your secret key. It only
needs to know how to talk to your running server and any tokens required for authentication.
4. Conclusion
Page 8 of 10
Your .env file is for server-side configuration.
Postman environment is for client-side (API testing) configuration (e.g., baseUrl , token ).
So, you do not import your .env file into Postman. Instead, make sure your Node server is reading
.env correctly, then set up Postman environment variables for the URL and any tokens you need to
pass in requests.
Short Answer:
No, you don't import the .env file into Postman. Run your server locally with those environment
variables, then set up a Postman environment that references the server's URL (and tokens, if
necessary).
i want to get the api endpoint and token for discount coupons
feature
Here's a quick outline on how you can get a token (for authentication) and call the discount
coupons endpoints in Postman:
1. Identify Your Auth/Login Endpoint
1. Find or create a login/auth route in your Node/Express app that returns a JWT or some kind of
token when the user logs in (e.g., POST /api/auth/login ).
2. In Postman, create a new request:
Method: POST
URL: {{baseUrl}}/api/auth/login (replace {{baseUrl}} with your server's URL, e.g.
http://localhost:3000 )
Body: Include the required fields (e.g., email/password or username/password).
3. Capture the token:
Page 9 of 10
In the Tests tab of this request, add something like:
js
let jsonData = pm.response.json();
pm.environment.set("token", jsonData.token);
This will store the returned token in a Postman environment variable called token .
2. Use the Token for Discount Coupons Requests
1. Check your Node/Express routes to see how you're exposing the “discount coupons” feature.
Common patterns:
GET /api/coupons to list all coupons
POST /api/coupons to create a new coupon
GET /api/coupons/:id to retrieve a specific coupon
etc.
2. Create a request in Postman for the relevant coupon route (e.g., {{baseUrl}}/api/coupons ).
3. Add the Authorization header:
Go to Headers in Postman.
Add a new header:
Key: Authorization
Value: Bearer {{token}}
4. Send the Request to see if you can access the discount coupons endpoint. If your server is set
up to require a valid token, you should get a successful response only if the token is valid.
3. Verify and Automate
Check Response: Inspect the response body, headers, and status code in Postman to ensure
you're getting the data you expect.
Chain Requests: If you have multiple endpoints to test (e.g., create a coupon, then retrieve it),
you can run them in sequence with the Collection Runner or Newman CLI tool.
Page 10 of 10
Tips
Do Not Import Your .env into Postman. Your .env is for the server (Node). Postman only needs:
The base URL (e.g. http://localhost:3000 )
Any tokens or other dynamic variables required for testing requests.
Environment Variables in Postman are optional but convenient. You can store things like
baseUrl and token in your Postman environment to avoid hardcoding them in every request.
Use Test Scripts to dynamically store or update values (e.g., newly created coupon IDs).
By following these steps, you'll be able to obtain a valid token via your login endpoint, store it in
Postman, and use it in the Authorization header when you call your discount coupons endpoints.