Introducing a new way to review your PRs.

Read the announcement

solana-foundation / solana-go

Go SDK library and RPC client for the Solana Blockchain

View on GitHub

26 documents · Updated Nov 16, 2026

docs / jsonrpc-transport

JSON-RPC transport

The JSON-RPC transport is the reusable HTTP layer that turns a method name and parameters into a JSON-RPC 2.0 request and turns the server reply into a typed response or error. It sits below the Solana RPC client methods, so those methods can focus on domain inputs and outputs instead of HTTP details. For the client that owns this layer, see RPC client; subscription traffic uses a separate path described in WebSocket subscriptions, while transaction calls use the same transport as described in Transaction submission.

Transport model

The transport exposes one endpoint-based client for single calls, raw calls, typed output calls, and batches. Each call uses an HTTP POST request with JSON content and carries the caller's context to the network operation.

Request lifecycle

The request lifecycle combines a JSON-RPC request object with HTTP metadata. A request receives JSON-RPC version 2.0, a correlation ID, and a parameter value that follows the JSON-RPC array-or-object rule before the transport sends it to the configured endpoint.

The flow from a typed method to the server is:

flowchart LR
    A[Typed RPC method] --> B[Method name and parameters]
    B --> C[JSON-RPC request]
    C --> D[HTTP POST]
    D --> E[RPC endpoint]

The transport sets Content-Type and Accept to application/json. A caller can provide a custom HTTP client for network settings and custom headers for authentication or routing. The request context controls cancellation and deadlines.

References: rpc/…/jsonrpc.go• L34-84 rpc/…/jsonrpc.go• L270-309 rpc/…/jsonrpc.go• L359-397 rpc/…/jsonrpc.go• L503-530

Request shapes

The request builder keeps common method calls concise while preserving valid JSON-RPC parameter shapes. The Call family applies this rule to variadic parameters, and NewRequest or Params applies the same rule when a caller needs to build a request object directly.

Caller inputJSON-RPC params value
No parametersField omitted
One primitive valueOne-element array
One object, struct, map, array, or sliceValue sent directly
More than one valueArray of values

CallRaw and CallBatchRaw bypass this shaping. They send the supplied request fields as provided, so the caller owns the JSON-RPC version, ID, and parameter validity. The regular batch call instead assigns a distinct index-based ID to each request and sets the version to 2.0.

References: rpc/…/jsonrpc.go• L137-193 rpc/…/jsonrpc.go• L465-500 rpc/…/jsonrpc.go• L679-747

Response lifecycle

The response lifecycle separates transport failure from a valid JSON-RPC error. The decoder accepts a response object with a raw result, an optional error, and an ID so the caller can choose when and how to decode the result.

The processing path is:

flowchart LR
    A[HTTP response] --> B{Valid JSON-RPC body?}
    B -- No --> C[Return parse or HTTP error]
    B -- Yes --> D[Build RPCResponse]
    D --> E{RPC error present?}
    E -- Yes --> F[Expose RPCError]
    E -- No --> G[Decode raw result when requested]

Malformed JSON, an empty body, and an unusable response body return an error. A valid JSON-RPC error remains in RPCResponse.Error; convenience methods return that RPCError to the caller. A JSON null result is treated as an absent result. Result decoding follows JSON unmarshalling rules and requires a pointer destination.

References: rpc/…/jsonrpc.go• L196-268 rpc/…/jsonrpc.go• L533-581 rpc/…/jsonrpc.go• L749-764

Batch coordination

Batch calls send several request objects in one HTTP body and return several response objects. The transport does not use response position as the business meaning of a result because servers can return batch responses in a different order.

Callers can inspect a batch by response ID or convert it to an ID-keyed map. HasError reports whether any response contains an RPC error. The normal batch API manages IDs for correlation, while the raw batch API requires the caller to manage them.

References: rpc/…/jsonrpc.go• L86-130 rpc/…/jsonrpc.go• L311-357 rpc/…/jsonrpc.go• L482-500 rpc/…/jsonrpc.go• L626-676

Typed method boundary

Typed RPC methods are the product-facing layer above the transport. They validate and group domain inputs, build the parameter list expected by one Solana RPC method, then ask the transport to decode the result into a domain type.

Method delegation

The main RPC client holds a narrow JSON-RPC client interface. A typed method such as GetBalance creates the method-specific parameter list and passes an output value to CallForInto; a transaction method follows the same path after it prepares its encoded transaction data.

This boundary keeps wire concerns in one place. Typed methods name the remote procedure and define its result shape, while the transport owns request IDs, JSON encoding, HTTP exchange, response parsing, and common error handling.

References: rpc/client.go• L37-47 rpc/client.go• L189-207 rpc/getBalance.go• L53-80 rpc/sendEncodedTransaction.go• L43-56

Result decoding

CallForInto returns a transport or network error first. If the server returns a JSON-RPC error, it returns that error without decoding a result. Otherwise it maps the response result into the output supplied by the typed method.

The output must be passed by reference. A null result can leave a pre-existing value unchanged or produce a nil pointer when the destination is a pointer-to-pointer. This behavior lets typed methods represent optional server values without adding a second transport format.

References: rpc/…/jsonrpc.go• L416-441 rpc/…/jsonrpc.go• L469-480 rpc/…/jsonrpc_test.go• L989-998 rpc/…/jsonrpc_test.go• L531-605

Transport use across RPC features

The same transport supports read calls, transaction simulation, and transaction submission. The feature method changes the method name and parameter content; the request and response contract remains shared.

Domain parameter flow

Domain methods prepare values before delegation. For example, balance lookup sends an account key with optional query settings, while transaction simulation and submission send encoded transaction data with operation settings. The transport then applies the common JSON-RPC and HTTP rules to both.

This relationship means a change to request shaping or response error handling can affect every typed RPC method. It also means callers can use the same endpoint and client lifecycle for read and write operations.

References: rpc/getBalance.go• L25-80 rpc/simulateTransaction.go• L134-194 rpc/sendEncodedTransaction.go• L25-56

Change checklist

When these files or sections are changed, remember to consider these:

  • Preserve the parameter-shape contract when changing Params, NewRequest, or any typed method that passes a parameter slice.
  • Keep batch response handling keyed by response ID. Do not assume that response order matches request order.
  • Keep CallForInto error precedence aligned with typed methods: network and parse errors must remain distinct from server RPCError values.
  • If response fields or decoding rules change, check both single-response and batch-response behavior. Both use strict JSON field handling.
  • If the injected JSON-RPC client interface changes, update the construction and delegation boundary in rpc/client.go together with all typed methods that use it.

Important failure modes

TriggerConsequenceGuard
A single primitive is passed as raw RPCRequest.ParamsThe server receives an invalid JSON-RPC parameter shapeUse Call, NewRequest, or Params unless raw wire control is required
A batch response is matched by array positionA result can be assigned to the wrong requestMatch responses by ID with GetByID or AsMap
The server returns malformed JSON or no bodyThe call returns a parse or missing-response error and no usable resultCheck the returned error before reading the response
The server returns a valid JSON-RPC errorTyped calls return RPCError instead of a decoded resultHandle RPCError as a server-side method failure
A non-pointer is supplied as the output destinationResult decoding failsPass a pointer to the target value
A typed method sends the wrong parameter order or option objectThe remote method rejects the call or produces an unintended resultTreat each typed method's parameter list as part of the RPC method contract

Key files

FileWhy to read it
rpc/…/jsonrpc.goDefines the transport interface, request and response shapes, parameter rules, HTTP exchange, batch behavior, and result decoding.
rpc/client.goShows how the product-facing RPC client creates and delegates to the reusable transport.
rpc/getBalance.goShows a read method converting domain inputs and options into transport parameters.
rpc/simulateTransaction.goShows a larger method preparing encoded data and options before decoding a structured result.
rpc/sendEncodedTransaction.goShows transaction submission using the same typed transport boundary.
rpc/…/jsonrpc_test.goDefines the transport contract for request shapes, headers, batch IDs, response parsing, errors, and result decoding.