Create and use gRPC mock servers

In an API-first design lifecycle, you may be designing a new feature that depends on new API endpoints, but another team is responsible for implementing those endpoints, and aren't finished yet. Instead of being blocked by this, you can use a mock server, which will return sample data for requests to the API's endpoints, simulating the behavior of a real API. You can then use the mock server to test your API, before it's even implemented by the other team.

Using a gRPC mock server

Before creating a gRPC mock server, you must have an API definition. This can either be an API in your workspace, a protobuf (protocol buffer) file you want to import, or you can create a new API using the Postman API Builder.

To create the gRPC mock server, do the following:

  1. Create a new gRPC request. See Creating a new request for more information.
  2. Select Enter server URL.
  3. Select Select a Protobuf API.
  4. Enter the protobuf API to use in one of the following ways:
    • Use an API from this workspace - Select an existing protobuf API in the current workspace.
    • Import a .proto file - Add a new protobuf API from a .proto file.
    • Create a new API - Open the Postman API Builder and create a new protobuf API.
  5. After you have selected an API, select Enter server URL again, and select Use Mock URL.

The mock server will be automatically generated, and ready for use. The request URL will be a server name beginning with a random name and ending in .srv.pstmn.io. Select methods, then select Invoke, and your request will receive responses with random values.

The mock server will dynamically update based on changes to your protobuf API. If changes are made in API Builder to the protobuf API, requests to the mock server will automatically implement the changes.

Reusing mock server responses

Each time you make a request against the mock server, different random data will be returned. However, if you are writing tests, you might need to get the same response every time. This is possible using the seed metadata in each response. The seed is a random number in the response metadata. If you pass the seed in a request, the same response will be returned each time.

To pass seed data, do the following:

  1. Make a gRPC request to a mock server.
  2. In the Metadata pane of the response, find the value of the key-value pair named seed. gRPC response seed
  3. In the Metadata pane of the request, set a key-value pair for seed with the value of the seed. gRPC request seed
  4. Invoke the request again.

Next steps

The mock servers in gPRC are independent of the mock server feature in Postman. See setting up mock servers for more information about using mock servers for your other APIs.

Last modified: 2022/09/15