gRPC and Protobuf Cheat Sheet: proto3 Syntax, Wire Types, Status Codes
Checked against Protocol Buffers and gRPC proto3 language guide and encoding docs on protobuf.dev; gRPC core concepts and status codes on grpc.io; encoding run with protobufjs 8.8.1 on 28 November 2026. Each section links to the official docs it was checked against.
This sheet is for developers writing .proto files and gRPC services who need the rules that break compatibility when you get them wrong. It follows the proto3 language guide and encoding docs on protobuf.dev and the gRPC docs on grpc.io. The byte dumps in the example were produced with protobufjs 8.8.1.
When to use what
The field number, not the name, is what goes on the wire. That single fact drives most of the rules: never change a number, never reuse a deleted one (mark it reserved), and you can rename fields freely as long as nobody relies on JSON output, which uses names.
Use optional on scalars when "not set" and "set to zero" mean different things, such as a discount of 0. Without it, proto3 can't tell them apart, because default values aren't encoded at all. The proto3 guide now recommends optional over implicit presence for new fields. Prefer it to the old google.protobuf.Int32Value style wrappers, which the reference calls mostly obsolete.
For gRPC methods, start with unary calls and add streaming only when the data really is a stream: server streaming for feeds and large result sets, client streaming for uploads, bidirectional for chat-like protocols. Always set a deadline on the client. To turn a JSON sample into a first draft of a message, try JSON to Protobuf; to read bytes you captured, paste them into the Protobuf wire decoder. Our JSON Schema validator helps when the same data also travels as JSON.
The syntax (or edition) line must be the first non-comment line. Without it, protoc assumes proto2.
syntax = "proto3";Select proto3; put it before anything else in the file
int32 id = 1;Declare a field; the number 1 is its identity on the wire, and in our test field 16 already needed a two-byte tag
1 .. 536,870,911Valid field numbers, except 19,000-19,999, which are reserved for the implementation
reserved 2, 9 to 11;Block old field numbers so nobody reuses them; ranges are inclusive
reserved "body";Block an old field name; numbers and names can't share one reserved line
double float int32 int64 uint32 uint64 sint32 sint64 fixed32 fixed64 sfixed32 sfixed64 bool string bytesThe proto3 scalar types; in our test int32 -1 took 10 value bytes and sint32 -1 took one
repeated int32 scores = 4;A list; numeric repeated fields are packed by default into one length-delimited record
map<string, string> labels = 5;A map; keys must be integral or string types, values can't be maps, and maps can't be repeated
oneof contact { string email = 7; string phone = 8; }At most one member is set; setting one clears the others; no maps or repeated fields inside
optional bool pinned = 6;Track presence, so an explicit false is sent and can be told apart from unset
enum Status { STATUS_UNSPECIFIED = 0; STATUS_OPEN = 1; }The first value must be 0 and is the default; name it _UNSPECIFIED
Each record starts with a varint tag computed as (field_number << 3) | wire_type.
0 VARINTint32, int64, uint32, uint64, sint32, sint64, bool, enum
1 I64fixed64, sfixed64, double: always 8 bytes
2 LENstring, bytes, embedded messages and packed repeated fields: a length then the bytes
3 SGROUP / 4 EGROUPGroup start and end; deprecated, don't use groups
5 I32fixed32, sfixed32, float: always 4 bytes
08 96 01Tag 0x08 = field 1, wire type 0; varint 96 01 = 150
google.protobuf.TimestampA UTC instant at nanosecond resolution; JSON form is an RFC 3339 string ending in Z
google.protobuf.DurationA signed span; JSON form is seconds with an s suffix, like "1.212s"
google.protobuf.EmptyA message with no fields, for methods with nothing to send or return
google.protobuf.FieldMaskPaths of fields to read or update; JSON form is a comma-separated lowerCamelCase string
google.protobuf.AnyAny message plus a type URL; JSON adds an @type key
google.protobuf.Struct / ValueDynamic JSON-like data when the shape isn't known in advance
Status codes are listed at https://grpc.io/docs/guides/status-codes/. Metadata keys starting with grpc- are reserved, and keys ending in -bin carry binary values.
rpc GetNote(GetNoteRequest) returns (Note);Unary: one request, one response
rpc ListNotes(ListRequest) returns (stream Note);Server streaming: one request, a sequence of responses
rpc Upload(stream Chunk) returns (UploadResult);Client streaming: many requests, one response
rpc Chat(stream Msg) returns (stream Msg);Bidirectional: two independent streams
0 OK, 1 CANCELLED, 2 UNKNOWN, 3 INVALID_ARGUMENT, 4 DEADLINE_EXCEEDED, 5 NOT_FOUNDCodes 0-5; DEADLINE_EXCEEDED can arrive even if the server finished the work
6 ALREADY_EXISTS, 7 PERMISSION_DENIED, 8 RESOURCE_EXHAUSTED, 9 FAILED_PRECONDITION, 10 ABORTED, 11 OUT_OF_RANGECodes 6-11; retry ABORTED at a higher level, don't retry FAILED_PRECONDITION until the state is fixed
12 UNIMPLEMENTED, 13 INTERNAL, 14 UNAVAILABLE, 15 DATA_LOSS, 16 UNAUTHENTICATEDCodes 12-16; UNAVAILABLE is the one to retry with back-off
Worked example: Encode a message and read the bytes
Define a message with a reserved slot, a packed list, a map and an optional flag:
syntax = "proto3";
package demo;
message Note {
reserved 2;
reserved "body";
int32 id = 1;
string title = 3;
repeated int32 scores = 4;
map<string, string> labels = 5;
optional bool pinned = 6;
}
Encode {id: 150, title: "hi", scores: [1, 2, 300], pinned: false} with protobufjs:
08 96 01 1a 02 68 69 22 04 01 02 ac 02 30 00
Read it record by record. 08 96 01 is field 1 as a varint with value 150, the same bytes as the encoding guide's example. 1a is field 3, wire type 2, then length 02 and the bytes 68 69 ("hi"). 22 is field 4 as LEN: the three scores are packed into four bytes, 01, 02 and ac 02 (300 needs two varint bytes). 30 00 is field 6 set to false; it appears only because the field is optional. Without that keyword, false is the default and would be left out. We checked the default rule directly: a message with only id: 0 encoded to zero bytes. The empty labels map is also absent. Decoding the 15 bytes gave back the same object.
Common mistakes
Deleting a field and later adding a new field with the same number.
Add the old number and name to
reservedwhen you delete a field, so protoc rejects reuse.Using
int32for a value that is usually negative, like a temperature offset.Use
sint32. Encoding -1 with protobufjs took ten value bytes asint32and one byte assint32.Expecting proto3 to tell you that a plain
int32 count = 1;was explicitly set to 0.Declare it
optional int32 count = 1;so presence is tracked and the zero is sent.Making the first enum value meaningful, such as
ACTIVE = 0.Reserve 0 for
..._UNSPECIFIED. Every unset field reads as 0, and you don't want that to look like a real state.Calling gRPC methods without a deadline.
Set a deadline or timeout on every client call so a stuck server ends in
DEADLINE_EXCEEDEDinstead of a hung request.
Related cheat sheets
Sources
- https://protobuf.dev/programming-guides/proto3/
- https://protobuf.dev/programming-guides/encoding/
- https://protobuf.dev/reference/protobuf/google.protobuf/
- https://grpc.io/docs/what-is-grpc/core-concepts/
- https://grpc.io/docs/guides/status-codes/
How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.