How do external gRPC clients utilize custom headers for authentication and tracing during Pub/Sub API calls?
Answer
Basics: Pub/Sub API (endpoint api.pubsub.salesforce.com:7443) lets external systems publish and subscribe to platform events, change events and real-time events using gRPC with HTTP/2 and Avro-encoded payloads. gRPC metadata is the equivalent of HTTP headers.
How it works:
- Get an OAuth access token and instance URL (for example with the JWT bearer flow).
- Open a TLS channel to the Pub/Sub endpoint.
- Attach the metadata on each call:
accesstoken: <token>,instanceurl: <https://MyDomain.my.salesforce.com>,tenantid: <18-char org ID>. - Call
GetTopicandGetSchemato learn the schema, thenSubscribewith aFetchRequest(topic name, replay preset, number of events requested). - Decode Avro payloads, process, and request more events to keep flow control going.
Advanced / deep dive: For tracing, add your own metadata (for example x-correlation-id or a W3C traceparent) through gRPC interceptors so your side's logs and gateway link up. Do not assume Salesforce echoes or propagates them into event payloads (verify). For end-to-end tracing across systems, put a correlation ID in the event payload field. Renew tokens before they expire, because a call with an expired token fails with an authentication error. Flow control matters: the client controls how many events it requests, so a slow consumer will not be flooded. Use ReplayPreset LATEST, EARLIEST or CUSTOM with a stored replay ID. Managed subscriptions that store the replay position in Salesforce are available in newer releases (verify status).