File Structure

gRPC APIs should be defined in .proto file(s) using the proto3 IDL.

The file structure must put higher level and more important definitions before lower level and less important items. In each proto file, the applicable sections should be in the following order:

  • Copyright and license notice if needed.
  • Proto syntax, package, option and import statements in that order.
  • The API overview documentation which prepares the readers for the rest of the file.
  • The API proto service definitions, in decreasing order of importance.
  • The resource message definitions. A parent resource must be defined before its child resource(s).
  • The RPC request and response message definitions, in the same order of the corresponding methods. Each request message must precede its corresponding response message, if any.

If a single proto file contains the entire API surface, it should be named after the API:

API Proto
Library library.proto
Calendar calendar.proto

Large .proto files may be split into multiple files. The services, resource messages, and request/response messages should be moved into separate files as needed.

We recommend one file per service with its corresponding request and responses. Consider naming this file <enclosed service name>.proto. For proto files with only resources, consider naming this file simply as resources.proto.

Proto File Names

Proto file names should use lower_case_underscore_separated_names and must use the extension .proto. For example: service_controller.proto.

Proto Options

In order to generate consistent client libraries across different APIs, API developers must use consistent proto options in their .proto files. API definitions that conform to this guide must use the following file-level proto options:

syntax = "proto3";

// The package name should start with the company name and end with
// the major version.

// This option specifies the namespace to be used in C# code. This defaults
// to the PascalCased version of the proto package, which is fine if the
// package name consists of single-word segments.
// For example, a package name of "" would use a C#
// namespace of "Google.Shopping.Pets.V1".
// However, if any segment of a package name consists of multiple words,
// this option needs to be specified to avoid only the first word being
// capitalized. For example, a Google Pet Store API might have a package name of
// "", which would mean a C# namespace of
// "Google.Shopping.Petstore.V1". Instead, the option should be used to
// capitalize it properly as "Google.Shopping.PetStore.V1".
// For more detail on C#/.NET capitalization rules, see the [Framework Design
// Guidelines](
option csharp_namespace = "Google.Abc.Xyz.V1";

// This option lets the proto compiler generate Java code inside the package
// name (see below) instead of inside an outer class. It creates a simpler
// developer experience by reducing one-level of name nesting and be
// consistent with most programming languages that don't support outer classes.
option java_multiple_files = true;

// The Java outer classname should be the filename in UpperCamelCase. This
// class is only used to hold proto descriptor, so developers don't need to
// work with it directly.
option java_outer_classname = "XyzProto";

// The Java package name must be proto package name with proper prefix.
option java_package = "";

// A reasonable prefix for the Objective-C symbols generated from the package.
// It should at a minimum be 3 characters long, all uppercase, and convention
// is to use an abbreviation of the package name. Something short, but
// hopefully unique enough to not conflict with things that may come along in
// the future. 'GPB' is reserved for the protocol buffer implementation itself.
option objc_class_prefix = "GABCX";

Send feedback about...