Testing with Postman | SuperTokens Docs (2024)

The following guide will go over the process of testing backend APIs with Postman. These APIs are automatically exposed via our backend SDK (/auth/* path).

important
  • Make sure that the Session Recipe is correctly setup in your backend
  • For the examples given below we will be running our backend on domain localhost and port 3001
  • The Open API spec for the APIs being tested can be found here.
  • Postman does cookie management on its own. So you don't need to manually set cookies on each request.

note

We recommend using cookie-based sessions while testing with Postman, to make use of the built-in cookie manager. This can be enabled by adding the "st-auth-mode: cookie" request header during the sign up API call as shown below.

You do not need to do this in your frontend code since our SDK does this for you.

1. Create a new Session#

We will first test creating a new session and check that session tokens are set.

For example, in your backend API you can use the Session.createNewSession function and check that the session tokens are set in the response through Postman.

  • NodeJS
  • GoLang
  • Python
  • Other Frameworks
import express from "express";
import Session from "supertokens-node/recipe/session";
import supertokens from "supertokens-node";

let app = express();

// in you backend
app.post("/create-new-session", async (req, res) => {
await Session.createNewSession(req, res, "public", supertokens.convertToRecipeUserId("test-user"), {}, {})
res.send({
"message": "New user session created"
})
})
  • In Postman, set the request type to POST.

  • In the Header tab, set key st-auth-mode with value cookie. This will advise the backend that you prefer cookie-based sessions. This can be overridden by backend settings, but it's respected by default.

  • On a successful request, a new user session will be created, with session tokens being returned in the response.

Testing with Postman | SuperTokens Docs (1)

You can see the session tokens set by the response by switching to the cookies tab

Testing with Postman | SuperTokens Docs (2)

These cookies are:

  • sAccessToken
  • sRefreshToken

More information about these cookies can be found here

2. Session Verification#

We can also test APIs that require the user to be logged in.

For example, we have an API used to query user data with the verifySession middleware as shown below.

  • NodeJS
  • GoLang
  • Python
  • Other Frameworks
import express from "express";
import { verifySession } from "supertokens-node/recipe/session/framework/express";
import { SessionRequest } from "supertokens-node/framework/express";

let app = express();

// The following code snippet is an example API. You do not need to
// implement it in your app

app.post("/change-user-data", verifySession(), async (req: SessionRequest, res) => {
let userId = req.session!.getUserId();
// mutate some user data
res.send({
userId
})
})
  • In Postman, set the request type to POST.
  • Set the URL to http://localhost:3001/change-user-data
  • If you have the antiCsrf attribute set to VIA_TOKEN in your backend SuperTokens config, then, in the Postman Header tab, set the key as anti-csrf and value as the anti-csrf token retrieved from the login response.
  • On a successful response, the response body will contain user data.

important

By default, for GET APIs, you don't need to provide the anti-csrf request header as anti-CSRF checks are only done in non-GET APIs

Testing with Postman | SuperTokens Docs (3)

In case you query the /change-user-data API with an expired access token, you will get a 401 response with the message try refresh token.

Testing with Postman | SuperTokens Docs (4)

To generate new session tokens you can use the /auth/session/refresh API as shown in the next section.

3. Refreshing Session Tokens#

In case your access token expires you can call the /auth/session/refresh api to generate a new access token and refresh token.

  • In Postman, set the request type to POST.
  • Set the URL to http://localhost:3001/auth/session/refresh
  • On a successful response, new session tokens will be set

Testing with Postman | SuperTokens Docs (5)

You can see the new session tokens by switching to the cookies tab

Testing with Postman | SuperTokens Docs (6)

4. Logout#

The /auth/signout API will be used to invalidate the user sessions. This will clear the session cookies set in postman.

  • In Postman, set the request type to POST.
  • Set the URL to http://localhost:3001/auth/signout
  • On a successful response, the session tokens will be cleared from Postman, and from the database

Testing with Postman | SuperTokens Docs (7)

Testing with Postman | SuperTokens Docs (2024)
Top Articles
855 Area Code: Everything You Need to Know About Toll-Free Numbers - CMS
Does Opening a Bank Account Affect Your Credit Score?
Knoxville Tennessee White Pages
Co Parts Mn
Www.megaredrewards.com
Nyuonsite
Texas (TX) Powerball - Winning Numbers & Results
All Obituaries | Ashley's J H Williams & Sons, Inc. | Selma AL funeral home and cremation
Moe Gangat Age
The Blind Showtimes Near Showcase Cinemas Springdale
Slmd Skincare Appointment
Busted Newspaper S Randolph County Dirt The Press As Pawns
Bowlero (BOWL) Earnings Date and Reports 2024
This Modern World Daily Kos
800-695-2780
The Superhuman Guide to Twitter Advanced Search: 23 Hidden Ways to Use Advanced Search for Marketing and Sales
Kvta Ventura News
Officialmilarosee
Energy Healing Conference Utah
Outlet For The Thames Crossword
Iroquois Amphitheater Louisville Ky Seating Chart
Bekijk ons gevarieerde aanbod occasions in Oss.
What Channel Is Court Tv On Verizon Fios
Doublelist Paducah Ky
Ac-15 Gungeon
Xfinity Outage Map Fredericksburg Va
Coomeet Premium Mod Apk For Pc
Target Minute Clinic Hours
Move Relearner Infinite Fusion
Turbo Tenant Renter Login
30+ useful Dutch apps for new expats in the Netherlands
Where to eat: the 50 best restaurants in Freiburg im Breisgau
950 Sqft 2 BHK Villa for sale in Devi Redhills Sirinium | Red Hills, Chennai | Property ID - 15334774
Kacey King Ranch
Dentist That Accept Horizon Nj Health
Nacogdoches, Texas: Step Back in Time in Texas' Oldest Town
Rvtrader Com Florida
Chris Provost Daughter Addie
The Boogeyman Showtimes Near Surf Cinemas
Culver's of Whitewater, WI - W Main St
Trap Candy Strain Leafly
Hireright Applicant Center Login
Ucla Basketball Bruinzone
Kaamel Hasaun Wikipedia
Rick And Morty Soap2Day
Identogo Manahawkin
Used Auto Parts in Houston 77013 | LKQ Pick Your Part
Cars & Trucks near Old Forge, PA - craigslist
Inloggen bij AH Sam - E-Overheid
Mast Greenhouse Windsor Mo
Cbs Scores Mlb
Latest Posts
Article information

Author: Carmelo Roob

Last Updated:

Views: 5974

Rating: 4.4 / 5 (45 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Carmelo Roob

Birthday: 1995-01-09

Address: Apt. 915 481 Sipes Cliff, New Gonzalobury, CO 80176

Phone: +6773780339780

Job: Sales Executive

Hobby: Gaming, Jogging, Rugby, Video gaming, Handball, Ice skating, Web surfing

Introduction: My name is Carmelo Roob, I am a modern, handsome, delightful, comfortable, attractive, vast, good person who loves writing and wants to share my knowledge and understanding with you.