MTS

Build REST API with Apigee and gRPC

01 December, 2021

3 min read

Build REST API with Apigee and gRPC

How to build REST API in a few minute using Google Apigee and gRPC

Today so many companies uses gRPC as standard for API, but this is not an article about how this technology can improves performances or how you can implement it: I’m going to talk about how to expose REST API, building only gRPC API in your server.

Using gRPC in my work I discovered how difficult it is to maintain both gRPC API and REST API, in fact there is more effort to update both APIs or it is very difficult to debug them.

We find a very good solution using Google Apigee.

What is Apigee?

Apigee is a Full Life Cycle API Management of Google and it is named leader in 2021 Gartner Magic Quadrant, in front of MuleSoft and Kong.

With Google Apigee you can:

  • Build and scale API programs with full API life cycle capabilities
  • Drive consumption of APIs with flexible developer portal options
  • Gain actionable insights across the entire API value chain
  • Monetize API products and maximize the business value of your digital assets

How it works

Apigee is a powerful tool but basically he exposes REST API for you and redirect the traffic in your backend and, then, serves your response to client. If you want to simplify the concept we can say that is a “API proxy”, however you can perform a lot of transformation or build very complex flow. For example you can perform an API Key authentication or collect insights and data of usage, even use others Google products like Firestore or Dialogflow.

Hands on

First of all we need a project in Google Cloud Platform* where we have to enable these API:

  • Apigee API;
  • Compute Engine API;
  • Service Networking API;

Now we can finally start our Apigee Evaluation Sandbox and follow the wizard to set up the environment.

*If you don’t have any GCP project, don’t worry: you can create it with 300$ of free credits

The first thing we need is a server so, in our Apigee project, we have to configure a it: Admin -> Environments -> Target Servers.

Here we have to select gRPC :

Remember the name, because we will use soon. Then, using an openapi.yaml file, we can create an API Proxy. Select “Use OpenAPI Spec” in Reverse proxy:

Now we can configure gRPC mapping for each REST API: open your API Proxy and select “Develop” tab, then select first API:

In “Request” we can add a new step “External Callout” where we can configure our mapping:

<ExternalCallout enabled="true" continueOnError="false" name="ExternalCallout-1">
  <DisplayName>My External Callout</DisplayName>
  <TimeoutMs>5000</TimeoutMs>
  <GrpcConnection>
    <Server name="mybackend"/>
  </GrpcConnection>
  <Configurations>
    <Property name="with.request.content">true</Property>
    <Property name="with.request.headers">false</Property>
    <Property name="with.response.content">true</Property>
    <Property name="with.response.headers">false</Property>
  </Configurations>
<ExternalCallout>

Note: at line 6 I used the name of “Target Server” configured above.

In this way we are going to map request and response content, but not the request and response headers, in the request sent to the gRPC server.

We must perform this action for each endpoint that we want expose or offer as REST API.

Conclusions

In this way we can develop only one type of API using gRPC and with Apigee provides REST API, and also we can check Authentication in advance or provides API Key, even collects statistics of usage of our API.

See you space cowboy…

Related posts

How to use Gitlab Feature Flags into Angular

How to use Gitlab Feature Flags into Angular

A small guide to configure Gitlab Feature Flags, Angular and Unleash

11 November, 2021