Get exchange rates in Go with net/http
Build a small, typed Go client for exchangerate.dev with net/http, context timeouts, environment-based authentication, and explicit freshness fields.
The standard library is enough for a reliable Go integration. Keep the API key in the process environment, reject non-2xx responses before decoding JSON, and retain source, market_session, and data_updated_at beside every rate.
Create a typed client with net/http
Save this complete program as main.go. It requests three symbols in one call and preserves numeric tokens as json.Number; use an exact-decimal library before doing money arithmetic. The anonymous request is useful for evaluation; set EXCHANGERATE_API_KEY when the application has a key. context.WithTimeout prevents a stalled network connection from holding a worker forever.
Call it with a timeout
Handle errors and freshness explicitly
Do not decode an error body as if it were a successful rate response. A 401 means the key or authorization header needs correction. A 429 means the caller should slow down and respect its plan. Retry only bounded, transient failures such as a network timeout or selected 5xx responses, with backoff.
source, market_session, and data_updated_at with the number. Actively traded currencies can update intraday on trading days; daily-reference currencies do not become live because the client polled more often.- Reuse an http.Client with an explicit transport timeout in production.
- Cache only successful responses.
- Use decimal arithmetic for money calculations at the application boundary.
- Never expose EXCHANGERATE_API_KEY in browser or mobile builds.