Testing gRPC APIs with Postman
Tags: api, postman, testing, tutorial
Published: 2022-08-11 08:21:26

---
title: Testing gRPC APIs with Postman
published: true
date: 2022-08-11 03:07:11 UTC
tags: api, postman, testing, tutorial
canonical_url: https://blog.postman.com/testing-grpc-apis-with-postman/
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/51e1r3iwapmp4ff8mzch.png
---

This article was written by [Appurva Murawat](https://blog.postman.com/author/appurva-murawat/).

Testing an API ensures that it consistently delivers the necessary functionality, performance, reliability, and security you need. It gives you the confidence to develop and ship faster, speeding up discoverability by API consumers. But it can be tedious, inefficient, and, more importantly, unreliable to manually test each time you add a new feature or fix a bug in your APIs. That’s what we’ve worked to solve for [Postman’s gRPC support.](https://blog.postman.com/postman-now-supports-grpc/)

Postman announced first-class support for gRPC at the beginning of this year and followed it up with [loads of improvements](https://blog.postman.com/latest-advancements-to-postmans-grpc-support/) based on some truly fantastic feedback we received from our gRPC community on [GitHub](https://github.com/postmanlabs/postman-app-support/issues?q=is%3Aissue+label%3Agrpc+is%3Aopen). And now, with the latest release, we’ve supercharged our gRPC support by adding the capability of writing test specifications for all your use cases. We hope these improvements will make your workflow even more efficient by allowing easier development and testing of your APIs.

## New yet familiar

Postman strives to provide the most straightforward and powerful user interfaces so that you can focus on the actual task without thinking too hard about navigating new territory. We bring this philosophy to the new gRPC testing interface as well. If you have ever used Postman to debug and [write tests for your REST APIs](https://learning.postman.com/docs/writing-scripts/test-scripts/), guess what, those are the same concepts you need to know for writing tests for your gRPC requests in Postman. If you’re a newcomer, don’t worry about it, we’ve got you covered: check out the [Postman Learning Center](https://learning.postman.com/labs/postman-api-client/grpc-client/writing-scripts/scripting-in-grpc-request/) to get started.

To add a test, open your gRPC request and switch to the  **Scripts**  tab. You can use Postman’s robust Javascript-based scripting environment to author test suites. You can add two different scripts:

- **Before invoke** : executes before invoking the method and establishing a connection with the server
- **After response** : executes once the current transaction is complete (i.e., connection with the server is closed)

![Navigating to scripts in the gRPC request](https://blog.postman.com/wp-content/uploads/2022/08/navigating-to-grpc-scripts.gif)
_Navigating to scripts in the gRPC request_

Once you’ve written the tests (more on that in a moment), you can observe the test assertions summary beside your response(s) under the **Test results**  tab:

![Invoking a gRPC request with tests](https://blog.postman.com/wp-content/uploads/2022/08/invoke-grpc-request-with-tests.gif)
_Invoking a gRPC request with tests_

## Get off the blocks in no time

Let’s start by writing our first test, so you can quickly see things in action:

1. Go to the  **After**  **response** script of your gRPC request.

2. From the list of  **Snippets ** on the right, choose **Status code is 0.**

3. Click  **Invoke.**

There you have it, your first test!

Both before invoke and after response scripts come with pre-curated lists of commonly used code [snippets](https://learning.postman.com/labs/postman-api-client/grpc-client/writing-scripts/writing-tests/#using-snippets) that get added to your scripts when selected, helping you start quickly.

![Authoring after response script using a snippet](https://blog.postman.com/wp-content/uploads/2022/08/using-snippets-in-grpc-scripts-1.gif)
_Authoring after response script using a snippet_

## Speed and accuracy superpower

Snippets are excellent for primary use cases, but as you can imagine, there might be more complex and custom scenarios that you want to test. For that, you can use the complete range of [pm.\* APIs](https://learning.postman.com/labs/postman-api-client/grpc-client/writing-scripts/postman-sandbox-api/) to craft your scripts. Check out the below tests to get a taste of all the things that you can do:

```
pm.test('Response time is below 200ms', function () { 
  pm.response.to.have.responseTime.below(200); 
});

pm.test('Correct user details are recevied', function () {
  pm.response.to.have.message({ id: "123", name: "John" });
});
```

You can even validate the messages against a given JSON schema, allowing you to enforce a number of custom rules:

```
const schema = { 
  type: "object", 
  properties: { 
    name: { 
      type: "string", 
      pattern: "^[a-zA-Z0-9_]*$", 
      maxLength: 64 
    } 
  }, 
  required: ['name'] 
}; 

pm.test('All response messages have valid name', function () { 
  pm.response.messages.to.have.jsonSchema(schema); 
});
```

As engineers, we understand that it might not always be efficient for you to refer to the documentation—that’s where the autocompletion for Javascript and the pm.\* APIs in our script editor comes in, empowering you with all the information you need to author your scripts swiftly and accurately. Hopefully, this saves you some trips to Stack Overflow as well.

![Autocompletion in gRPC scripts](https://blog.postman.com/wp-content/uploads/2022/08/autocompletion-in-grpc-scripts.gif)
_Autocompletion in action_

## Leave nothing to chance

All the features and workflows mentioned above extend to unary, client streaming, server streaming, and bidirectional streaming gRPC methods alike. This makes it easier to have test specifications for all your gRPC APIs in a single place—Postman!

## More to come

The ability to test your gRPC APIs is a powerful addition to the [Postman API Platform](https://blog.postman.com/new-postman-api-platform-redefining-api-management-for-api-first-world/), and it’s another way we’re working to help users throughout the [API lifecycle](https://dev.to/jansche/the-8-point-api-lifecycle-blueprint-3op4-temp-slug-9748578). And gRPC support continues to evolve. Keep an eye out for more upcoming releases, including:

- Saving examples for gRPC request
- Automatic mock servers for all imported Protobuf APIs
- Automating your gRPC testing workflows using Postman’s [Collection Runner](https://learning.postman.com/docs/running-collections/intro-to-collection-runs/)

The post [Testing gRPC APIs with Postman](https://blog.postman.com/testing-grpc-apis-with-postman/) was written by Appurva Murawat and appeared first on [Postman Blog](https://blog.postman.com).