Compile Protocol Buffers using Buf
Overview
With Buf, using a simple and declarative configuration, you can compile Protocol Buffers into language-specific source code, including gRPC client stubs. This approach is beneficial when working with Sift’s gRPC API, where the Protocol Buffers define the data structures and the service interfaces needed to generate a client.
Sift provides official gRPC client libraries for Python, Rust, and Go. If your preferred language is not one of these, such as TypeScript, Java, or C#, you can compile Sift’s Protocol Buffers using Buf to generate a compatible client library in that language.
Although there are several ways to compile Protocol Buffers, this guide focuses on using Buf because of its ease of use, flexible plugin system, and support for multiple programming languages.
Instructions
To compile Sift's Protocol Buffers using Buf, consider the following steps:
These steps (example) use Python as the target language, but you can adapt the process to other languages by selecting different plugins.
-
Create a new directory for your Python client, and navigate into it. This directory will hold your Buf configuration and the generated client code.
-
Install the Buf CLI globally by following the instructions in the Buf installation instructions.
-
Clone the Sift GitHub repository to access the Protocol Buffer definitions locally. These files define the gRPC services and messages that will be compiled into your Python client.
The
.proto
files are located in the protos subdirectory of the repository. -
In your
sift-python-grpc-client
directory, create a file namedbuf.gen.yaml
. This file specifies the plugins that Buf should use and where the generated code should be written.This configuration generates Python message classes, gRPC client and server stubs, and type hint files. All output is written to the
gen/
directory by default, but you can modify theout
paths to change the output location.Many of these plugins may require additional runtime dependencies after the code has been generated to work properly. Visit the documentation for each plugin you use to learn what packages or libraries must be installed.
-
From inside the
sift-python-grpc-client
directory, run the following command to export the.proto
files from the Sift repository into a local directory:<OUTPUT_DIR>
is the path to the directory where the Protocol Buffer files will be emitted. For example,sift_protos
.<PATH_TO_SIFT_BUF_CONFIG>
is the path to thebuf.yaml
file located in the protos subdirectory of the cloned Sift repository (sift
).
After running the command, verify that the
.proto
files were successfully copied into thesift_protos
directory. -
In the
buf.gen.yaml
file directory, run the following command to generate code from the.proto
files you exported:Replace
<OUTPUT_DIR>
with the name of the directory containing Sift's Protocol Buffers. For example, if you usedsift_protos
, the command would be:If the command runs successfully, the generated code will be written to the location specified in the out fields of your
buf.gen.yaml
file, such as thegen/
directory.