Connecting Postman to Amazon Cognito User Pools for API Access Tokens
Tags: accesstoken, apigateway, authentication
Published: 2022-02-05 13:28:08

---
title: Connecting Postman to Amazon Cognito User Pools for API Access Tokens
published: true
date: 2021-10-26 23:21:08 UTC
tags: accesstoken,apigateway,authentication
canonical_url: https://mahmutcanga.com/2021/10/26/connecting-postman-to-amazon-cognito-user-pools-for-api-access-tokens/
---

![](https://sufidev.files.wordpress.com/2021/10/pexels-photo-4175032.jpeg)
_Photo by Khwanchai Phanthong on Pexels.com_

Majority of the time in my recent projects, I use [Amazon Cognito](https://aws.amazon.com/cognito/) for user authentication (sign in, sign up, login with identity providers etc) in front of an [Amazon API Gateway](https://aws.amazon.com/api-gateway/). Usually the API endpoints control access [using Amazon Cognito user pools as authorizer](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-integrate-with-cognito.html)

In these type of APIs, testing the API using [Postman](https://www.postman.com) is a good practice. Use of Postman helps distributing the API contracts easily while helping you as a developer to run different types of tests without a full-blown client implementation.

The expected way to connect and consume these APIs are providing an `id token` from Amazon Cognito authorization in the headers. This token is auto-validated by Amazon API Gateway by leveraging Cognito Authorizers. A typical request to these endpoints would look like below:

```bash
curl --location --request GET 'https://{API-ID}.execute-api.eu-west-2.amazonaws.com/v1/profiles/123' \
--header 'Authorization: eyJraWQiOiJOZjB0clFHanRG.....'
```

To add this token in every request, [Postman supports Collection](https://www.postman.com/collection/) based Authorization setting. (I assume you follow putting all requests under a collection in Postman). As you can see, Postman supports variety of authorization techniques.

![](https://sufidev.files.wordpress.com/2021/10/image.png?w=1024)

What we are going to use from these alternatives is **OAuth 2.0** which Amazon Cognito supports out of box. There is a detailed deep dive on different grant types available on [AWS Blog.](https://aws.amazon.com/blogs/mobile/understanding-amazon-cognito-user-pool-oauth-2-0-grants/)

As part of your Amazon Cognito setup, you are expected to create an App Client which has access to this user pool. Using this App Client, we will be able to sign in using an existing user and grab an `id token` that will be used for API calls.

## [Step 1] – App Client Id and Callback URL(s)

In order to setup this, go to App Client Settings section of the Cognito pool.

![](https://sufidev.files.wordpress.com/2021/10/app-client-settings.jpg?w=1024)

You will use the `App Client Id` and `Callback URL(s)` from this page in your OAuth 2.0 setup in Postman.

## [Step 2] OAuth 2.0 Flows

Also make sure the following OAuth 2.0 flows are enabled on the same App Client Settings page.

![](https://sufidev.files.wordpress.com/2021/10/image-2.png?w=1024)

The way to sign in with an existing user account is via [Hosted UI](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-app-integration.html) feature of Amazon Cognito. This UI is a very basic sign in/sign up screen that conforms the User Pool settings. It is actually a nice way of testing your Cognito setup out of box   
(nice as in available and working, not as in with a slick UI 🙂)

## [Step 3] Hosted UI Domain

As you may realise from above screenshot, Hosted UI needs to be setup. In order to do that, go to **App Integration** section and click **Add Domain**

![](https://sufidev.files.wordpress.com/2021/10/app-integration-settings.jpg?w=1024)

In Add Domain screen, you can set a domain name for your Hosted UI. This URL will be used for OAuth 2.0 settings in Postman. Also, this URL will be used for the sign-up and sign-in pages that are hosted by Amazon Cognito. The prefix must be unique across the selected AWS Region. Domain names can only contain lower-case letters, numbers, and hyphens. [Learn more about domain prefixes. ](http://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-assign-domain.html)

## [Step 4] Collection Authorization Settings

Next part is setting up Postman Collection in order to sign in / sign up and get an `id token` for making authenticated API calls. Fill in the following details by editing the Collection Authorization settings:

![](https://sufidev.files.wordpress.com/2021/10/postman-oauth2-settings.jpg?w=1018)

- **Grant Type:** Implicit
  - Uncheck `Authorise using browser` option
- **Callback URL:** [https://example.com](https://example.com)
- **Auth URL:** {Hosted UI URL}/login
- **Client ID:** {App Client Id}
- **Scope:** phone email openid profile aws.cognito.signin.user.admin
- **Client Authentication:** Send client credentials in the body

## [Step 5] Generate Access Token

When you enter these details and click **Get New Access Token** button, Postman will open the Hosted UI URL for you to sign in or sign up.

![](https://sufidev.files.wordpress.com/2021/10/hosted-ui-screen.jpg?w=1018)

What is nice about this setup is, you can just create a test user and use it for all API calls accordingly using the above UI. Above UI is enabled by setting Cognito Pool setting called `User sign ups allowed? Users can sign themselves up`

As you can see, this UI complies with the user pool settings and gives you a nice way to test your setup. Here is the screenshot of signup screen and look at the password policy in place….

![](https://sufidev.files.wordpress.com/2021/10/image-3.png?w=1024)

Hosted UI even provides the email and/or phone validation during setup. In my user pool setup, I require users to have validated emails. So, in this below screenshot I can see that Hosted UI shows related information and input. As Cognito sends real emails for verification code, I suggest to use a real email here to avoid any confusion…

![](https://sufidev.files.wordpress.com/2021/10/image-4.png?w=1024)

When you finish a successful sign in or sign up, Postman confirms this and grabs the token.

![](https://sufidev.files.wordpress.com/2021/10/image-5.png?w=1024)

Following screen is shown with the token details. You will see that this screen has an `Access Token` and an `id_token`.

> For API Gateway Cognito Authorizer workflow, you will need to use `id_token`.
> 
> <cite>!!! IMPORTANT DETAIL !!!</cite>

![](https://sufidev.files.wordpress.com/2021/10/manage-tokens-postman-1.jpg?w=1018)

Simply copy the value of `id_token` and put it in Access Token value of the Current Token setting. This will make the `id_token` available for all requests in that collection.

![](https://sufidev.files.wordpress.com/2021/10/image-6.png?w=1024)

## [Step 6] Setup Collection Authorization Settings

Last step is updating API requests to use the Collection Authorization settings. Setting the Authorization setting of requests as `Inherit auth from parent` will let Postman inject Access Token in the `Authorization` header value.

![](https://sufidev.files.wordpress.com/2021/10/request-auth-setting.jpg?w=1018)

## Conclusion

I hope you will be able to easily test your APIs behind Cognito using this setup via Postman. You can export the Collection as `.json` and even make part of your codebase so you will be embracing API Contract Testing as Code – ACTaC (I made this up now…)