A generics-first OCPI (Open Charge Point Interface) implementation in Go for OCPI 2.1.1, 2.2.1 and 2.3.0 — the HTTP/JSON REST protocol for e-mobility roaming between CPOs, eMSPs and Hubs.
Status: WIP, pre-v1. Implemented: the
coretransport (envelope, status codes, pagination, token auth, routing, retries, metrics), generated types + typed clients + server handler interfaces for every module of all supported versions, role presets, the Versions/Credentials handshake, and two-layer validation (struct-tag + JSON-Schema). The public API may change before v1.0.
gocpi is the OCPI sibling of gocpp (OCPP) and shares its philosophy: generics-first ergonomics, version-prefixed packages, codegen from vendored OpenAPI specs, pluggable observability, and heavy testing.
- Client + server in one module. OCPI parties are both at once: gocpi gives
you typed HTTP clients to call peers and
http.Handlerserver endpoints to host your own — for every role (CPO, eMSP, Hub, ...). - Generated from vendored OpenAPI specs. Types, clients and server handler
interfaces for every module of OCPI 2.1.1 (
v211), 2.2.1 (v221) and 2.3.0 (v230, incl. bookings & payments) are generated from vendored OpenAPI specs. The 2.2.1/2.3.0 specs come from the official OCPI OpenAPI specification; the 2.1.1 spec is community-derived and non-official. - OCPI semantics built in. The standard response envelope, OCPI status codes,
pagination (
Link/X-Total-Count/X-Limit+ iterators), Base64 token auth, the Versions+Credentials handshake, hub message-routing headers, and transient-failure retries. - Two-layer validation. Generated
validatestruct tags (core.Validate) plus embedded JSON-Schema validators (v211.ValidateJSON,v221.ValidateJSON,v230.ValidateJSON). - Framework-agnostic. The server is a plain
http.Handler— mount it under any prefix in net/http, chi, echo, etc. - Pluggable observability.
sloglogging and aMetricsinterface with Prometheus and OpenTelemetry adapters.
The github.com/shiv3/gocpi/core/pricing
package provides a version-neutral OCPI 2.2.1 and 2.3.0 tariff calculation and
CDR total verification engine. Version entry points live in the version
packages (v221, v230) and return the neutral pricing.Report / pricing.Verdict.
import (
pricing "github.com/shiv3/gocpi/core/pricing"
"github.com/shiv3/gocpi/v221"
)
// Primary path: price using the tariffs embedded in the CDR (resolves each
// charging period's tariff_id, including multi-tariff CDRs).
rep, err := v221.CalculateCDR(cdr, pricing.Options{})
verdict, err := v221.VerifyCDR(cdr, pricing.Options{})
// Override path: price every period against an explicit tariff, ignoring
// per-period tariff_id.
rep, err = v221.CalculateWithTariff(cdr, tariff, pricing.Options{})CalculateCDR/VerifyCDR resolve each charging period to its tariff_id from
the CDR's embedded tariffs, including CDRs that switch tariffs across periods.
The step_size ledger stays session-wide across tariff switches (the last priced
period's component supplies the step and prices the rounding delta). Periods with
no applicable tariff are excluded from cost but still advance the cumulative
restriction snapshots and the Verify volume audit.
Multi-tariff behaviors:
step_sizeis applied once per session; mixed step values across tariffs emitWarnMixedStepSize.- Each tariff's
FLATelement is charged once. min_price/max_priceare not applied when more than one tariff prices the session; the engine emitsWarnMinMaxUndefinedMultiTariffandVerifyreportstotal_costasNotVerifiable.- A period with a nil/unmatched
tariff_idis zero-cost withWarnPeriodNoTariff(multiple tariffs) or rejected asInvalidInput(single tariff / unknown id).
Current v1 limitations:
- Reservation cost is not computed; its sub-total verifies as
NotVerifiable. - OCPI 2.3.0 booking-restricted elements are unsupported and never match.
- The local-time boundary-crossing diagnostic is deferred.
- Multi-timezone countries are not inferred; pass
Options.TimeZone. - Totals stay at OCPI scale-4 unless
Options.CurrencyPrecisionis set.
go get github.com/shiv3/gocpiRequires Go 1.26+.
cpo := core.NewServer(core.WithBaseURL("https://cpo.example/ocpi"))
// Versions + Credentials handshake endpoints.
handshake.Mount(cpo.Mux(), handshake.ServerConfig{ /* versions, details, onRegister */ })
// Functional-module endpoints for the CPO role (Locations Sender, Tokens
// Receiver, ...). A nil handler field is skipped.
v221.RegisterCPO(cpo.Mux(), "https://cpo.example/ocpi/2.2.1", v221.CPOHandlers{
Locations: myLocationsSender, // implements v221.LocationsSenderHandler
})
http.ListenAndServe(":8080", cpo.Handler())Mount under any prefix (the handler does relative routing):
r.Handle("/ocpi/*", http.StripPrefix("/ocpi", cpo.Handler())) // chi
e.Any("/ocpi/*", echo.WrapHandler(http.StripPrefix("/ocpi", cpo.Handler()))) // echoemsp := core.NewClient(core.WithToken(tokenA), core.WithRetry(3))
// Register: discover versions, exchange credentials.
peer, err := handshake.Register(ctx, emsp, handshake.RegisterRequest{
PeerVersionsURL: "https://cpo.example/ocpi/versions",
PreferVersion: v221.VersionNumber221,
OurCredentials: ourCredentials,
})
// Call the discovered Locations endpoint with the generated typed client.
locURL, _ := peer.Endpoint(v221.ModuleIDLocations)
page, err := v221.NewLocationsSenderClient(emsp, locURL).GetLocations(ctx, core.PageOpts{})A complete, runnable version is in examples/dual-role.
core.Validate(loc) // struct-tag layer (go-playground/validator)
err := v221.ValidateJSON("Location", raw) // wire layer (JSON-Schema, embedded)cpo := core.NewServer(core.WithMetrics(prom.New(prometheus.DefaultRegisterer)))
emsp := core.NewClient(core.WithClientMetrics(otelm))| Import | Purpose |
|---|---|
core |
Response envelope, pagination, token auth, routing, request IDs, retries, base client/server/mux |
core/status |
OCPI status codes (1xxx–4xxx) + typed Error |
core/transport |
Doer HTTP abstraction + in-memory fake for tests |
core/schema |
JSON-Schema Validator |
core/observability (+ metrics/{prom,otel}) |
Metrics interface + Prometheus/OpenTelemetry adapters |
v211 / v221 / v230 |
Generated types, typed clients + server handlers, role presets, embedded JSON-Schema validator |
handshake |
Versions + Credentials registration |
| Package | OCPI version | Modules |
|---|---|---|
v211 |
2.1.1 | locations, sessions, cdrs, tariffs, tokens, commands, credentials, versions |
v221 |
2.2.1 | locations, sessions, cdrs, tariffs, tokens, commands, chargingprofiles, hubclientinfo, credentials, versions |
v230 |
2.3.0 | v221 modules + bookings, payments |
make test # go test ./...
make test-race # go test -race ./...
make generate # regenerate v211 + v221 + v230 from the vendored OpenAPI
make lint # golangci-lintSee docs/architecture.md for the design overview.
MIT