> ## Documentation Index
> Fetch the complete documentation index at: https://docs.formal.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# gRPC

> How to connect to a gRPC Resource using the Formal Connector

## Requirements

​

### Networking

Certain ports must be accessible to connect to Formal Resources. You can listen on multiple ports simultaneously for various Resources.

gRPC is a high-performance, open-source Remote Procedure Call (RPC) framework that uses HTTP/2 for transport, Protocol Buffers as the interface description language, and provides features like authentication, bidirectional streaming, and flow control. The Formal Connector enables you to proxy and monitor gRPC traffic to your APIs while enforcing policies and logging all interactions.

> [!WARNING]
>
> The connector runs a healthcheck server on port 8080 to let clients determine if the proxy is operational. As a result, you cannot create a listener on port 8080 to connect to a gRPC resource.

### Resource health check

Formal periodically checks that the Connector can open a TCP connection to the gRPC server. When [TLS](https://docs.formal.ai/docs/guides/core-concepts/resources/tls.md) is enabled for the Resource, the check also completes a TLS handshake. It does not send gRPC requests.

### Native Users

The Connector can inject upstream credentials with Native Users. gRPC Resources accept the HTTP credential types, such as **HTTP Bearer** and **API Key (Header)**. Header credentials reach the server as gRPC metadata.

To send each user's own OAuth token instead, use [linked accounts](https://docs.formal.ai/docs/guides/core-concepts/resources/oauth.md).

## Connect to a gRPC Resource

To connect to a gRPC resource with a Formal identity, set the following metadata in your gRPC calls:

* `x-formal-user-username`: The username of the user making the request
* `x-formal-user-password`: The user's authentication credentials

### Connecting using the gRPC CLI

To connect using `grpc_cli`, execute the following command:

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
grpc_cli call CONNECTOR_HOSTNAME:PORT GRPC_METHOD_NAME --metadata "x-formal-user-username:FORMAL_USERNAME:x-formal-user-password:FORMAL_ACCESS_TOKEN"
```

Make sure to replace `CONNECTOR_HOSTNAME`, `PORT`, `GRPC_METHOD_NAME`, `FORMAL_USERNAME`, and `FORMAL_ACCESS_TOKEN` with the right values.

## Smart Routing

The Connector features Smart Routing for gRPC, allowing the linkage of an
unlimited number of Resources over the same port.

Prefix the Connector hostname with the resource name:

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
grpc_cli call RESOURCE_NAME.CONNECTOR_HOSTNAME:PORT GRPC_METHOD_NAME \
  --metadata "x-formal-user-username:FORMAL_USERNAME:x-formal-user-password:FORMAL_ACCESS_TOKEN"
```

Replace `RESOURCE_NAME`, `CONNECTOR_HOSTNAME`, `PORT`, `GRPC_METHOD_NAME`,
`FORMAL_USERNAME`, and `FORMAL_ACCESS_TOKEN` with the right values.

If your gRPC client dials one address and sends a separate authority or server
name, set that value to `RESOURCE_NAME.CONNECTOR_HOSTNAME`.

> [!WARNING]
>
> Make sure your DNS entries are properly configured to handle the subdomain
> routing, i.e. that `*.CONNECTOR_HOSTNAME` targets your Connector. Use a
> wildcard TLS certificate that covers every resource subdomain.

## Policy Evaluation

Formal supports the following policy evaluation stages for gRPC:

* **Session**: Evaluate and enforce policies at connection time
* **Request**: Evaluate and enforce policies before request execution

## Policies and Controls

You can apply Formal policies to gRPC traffic at the **session** and **request** stages. The gRPC method name is available as `input.grpc.method_name`.


## Related topics

- [Smart Routing](https://docs.formal.ai/docs/guides/core-concepts/resources/smart-routing.md)
- [Formal API](https://docs.formal.ai/docs/api/introduction.md)
- [Create resource OAuth settings](https://docs.formal.ai/api-reference/corev1resourceservice/create-resource-oauth-settings.md)
- [OAuth and Linked Accounts](https://docs.formal.ai/docs/guides/core-concepts/resources/oauth.md)
- [Connector](https://docs.formal.ai/docs/changelog/connector.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.