Reference
The Protobuf language
Protobuf schemas are written in .proto files. This is a short tour of what goes in one; the full rules are in Google's
language specification for proto3 and
for Editions, and the
language guide.
syntax = "proto3";
package acme.payments.v1;
import "acme/common/v1/money.proto";
// A payment, from creation to settlement.
message Payment {
string id = 1;
acme.common.v1.Money amount = 2;
Status status = 3;
map<string, string> labels = 4;
}
enum Status {
STATUS_UNSPECIFIED = 0;
STATUS_PENDING = 1;
STATUS_SETTLED = 2;
}
service PaymentService {
rpc GetPayment(GetPaymentRequest) returns (GetPaymentResponse);
}Syntax and editions
A file starts by saying which version of the language it uses: syntax = "proto3";, the older syntax = "proto2";, or
an edition, as edition = "2023";. Editions replace the two syntaxes with one language whose behaviour is chosen by
features. Without the line, a file is proto2.
Packages and imports
package names the file's namespace, as acme.payments.v1; types in other packages are named in full. import makes
another file's types available, by its path within its module or a dependency (acme/common/v1/money.proto).
Messages
A message is a record of fields. Each field has a type, a name and a number. The number identifies the field in the
binary encoding, so it must never be reused for something else once the message is in use. Fields can be repeated
(a list), optional (presence is tracked), or grouped in a oneof, where at most one is set.
Field types
The scalar types are double, float, int32, int64, uint32, uint64, sint32, sint64, fixed32,
fixed64, sfixed32, sfixed64, bool, string and bytes. A field can also be a message or an enum. The
well-known types (google.protobuf.Timestamp, Duration, Any, Struct …) are messages every Protobuf runtime
ships, imported from google/protobuf/.
Maps
map<K, V> is a field of key-value pairs. The key is an integer type, bool or string; the value is any type but
another map. A map can't be repeated.
Enums
An enum is a set of named numbers. In proto3 the first value must be zero, and is what an unset field reads as, so it's
usually named …_UNSPECIFIED. Enum values share their package's namespace, which is why they're prefixed with the enum's
name.
Services
A service groups RPCs. Each RPC takes one request message and returns one response; either can be a stream.
Options
Options annotate a file, message, field or other element, as option go_package = "…"; or [deprecated = true] on a
field. Custom options are extensions of the descriptor messages.
Symbol visibility
From Edition 2024, export and local say whether a message or enum can be used from other files: local ones are
only for the file that declares them. A file's default is set by a feature.
Comments
// and /* … */. A comment directly above an element is its documentation: Stubs's API Explorer shows it, and
code generators copy it into the generated code.