# Transcode HTTP to gRPC

[gRPC](https://grpc.io/) is a remote procedure call framework that uses HTTP/2 for transport and [Protocol Buffers](https://protobuf.dev/) to define services and message types.

The `grpc-transcode` plugin lets an HTTP client call a gRPC service through APISIX. APISIX encodes the HTTP request as a gRPC message, sends it to the configured method, and decodes the response as JSON.

<br />

![Flow in which APISIX converts an HTTP request to gRPC and returns the gRPC response as JSON](https://static.api7.ai/uploads/2023/05/06/omYvTUSm_transform-grpc.jpg)

<br />

## Prerequisite(s)[​](#prerequisites "Direct link to Prerequisite(s)")

* Install [Docker](https://docs.docker.com/get-docker/).
* Install [cURL](https://curl.se/) to send requests to APISIX for validation.
* Install [jq](https://jqlang.github.io/jq/) to embed the `.proto` definition in an Admin API request.
* Install [gRPCurl](https://github.com/fullstorydev/grpcurl) to call the example gRPC service directly.
* Follow the [Getting Started tutorial](https://docs.api7.ai/apisix/getting-started.md) to start APISIX in Docker. The commands below use the `apisix-quickstart-net` network created by that tutorial.
* If Admin API authentication is enabled, export an admin-role key as `ADMIN_API_KEY`. The Docker quickstart disables authentication for local evaluation; keep it enabled in production.

## Deploy an Example gRPC Server[​](#deploy-an-example-grpc-server "Direct link to Deploy an Example gRPC Server")

Start the [example gRPC server](https://github.com/api7/grpc_server_example) in the APISIX quickstart network. The image digest pins the tested contents of tag `1.0.2`:

```
docker rm -f quickstart-grpc-example 2>/dev/null || true



docker run -d --rm \

  --name quickstart-grpc-example \

  --network apisix-quickstart-net \

  -p 50051:50051 \

  api7/grpc-server-example:1.0.2@sha256:9d822f5957724d2b9f668e81d5cd221e030a7155ac5558b56f1a07cb9e8cbbce
```

The server exposes `echo.EchoService`. Its `Echo` method accepts an `EchoMsg` message and returns the same message type.

Call the method directly with gRPCurl:

```
grpcurl -plaintext \

  -d '{"msg":"Hello"}' \

  "127.0.0.1:50051" \

  "echo.EchoService/Echo"
```

The server should return the message:

```
{

  "msg": "Hello"

}
```

## Create a Proto Object[​](#create-a-proto-object "Direct link to Create a Proto Object")

Save the service definition as `echo.proto`:

echo.proto

```
cat > echo.proto <<'EOF'

syntax = "proto3";



package echo;



service EchoService {

  rpc Echo (EchoMsg) returns (EchoMsg);

}



message EchoMsg {

  string msg = 1;

}

EOF
```

Create a [proto object](https://docs.api7.ai/apisix/key-concepts/protos.md) with the ID `quickstart-proto`:

```
jq -n --rawfile content echo.proto \

  '{id: "quickstart-proto", content: $content}' \

  | curl -i "http://127.0.0.1:9180/apisix/admin/protos" -X PUT \

    -H "X-API-KEY: ${ADMIN_API_KEY}" \

    -H "Content-Type: application/json" \

    --data-binary @-
```

An `HTTP/1.1 201 Created` response verifies that APISIX stored the proto object.

## Configure `grpc-transcode`[​](#configure-grpc-transcode "Direct link to configure-grpc-transcode")

Create a route that maps `GET /echo` to `echo.EchoService/Echo`:

```
curl -i "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -H "Content-Type: application/json" \

  --data-binary @- <<'EOF'

{

  "id": "quickstart-grpc",

  "methods": ["GET"],

  "uri": "/echo",

  "plugins": {

    "grpc-transcode": {

      "proto_id": "quickstart-proto",

      "service": "echo.EchoService",

      "method": "Echo"

    }

  },

  "upstream": {

    "scheme": "grpc",

    "type": "roundrobin",

    "nodes": {

      "quickstart-grpc-example:50051": 1

    }

  }

}

EOF
```

❶ Load the service definition from the `quickstart-proto` proto object.

❷ Select the fully qualified gRPC service name.

❸ Select the method to invoke on that service.

An `HTTP/1.1 201 Created` response verifies that APISIX created the route.

## Verify HTTP-to-gRPC Transcoding[​](#verify-http-to-grpc-transcoding "Direct link to Verify HTTP-to-gRPC Transcoding")

Send an HTTP request with the `EchoMsg.msg` field as a query parameter:

```
curl -i "http://127.0.0.1:9080/echo?msg=Hello"
```

APISIX should return HTTP 200 with the decoded gRPC response:

```
{

  "msg": "Hello"

}
```

## Next Steps[​](#next-steps "Direct link to Next Steps")

The `grpc-transcode` plugin supports additional request and response options. See the [`grpc-transcode` Plugin Hub reference](https://docs.api7.ai/hub/grpc-transcode.md) for configuration details and examples.

For browser clients, APISIX supports [gRPC-Web](https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-WEB.md), which adapts the [native gRPC protocol](https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md). Configure it with the [`grpc-web` plugin](https://docs.api7.ai/hub/grpc-web.md).
