Zero-dependency Go SDK for PeekAPI. Standard net/http middleware plus adapters for Gin, Echo, Chi, and Fiber.
go get github.com/peekapi-dev/sdk-gopackage main
import (
"log"
"net/http"
peekapi "github.com/peekapi-dev/sdk-go"
)
func main() {
client, err := peekapi.New(peekapi.Options{
APIKey: "ak_live_xxx",
})
if err != nil {
log.Fatal(err)
}
mux := http.NewServeMux()
mux.HandleFunc("/api/hello", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"message":"hello"}`))
})
handler := peekapi.Middleware(client)(mux)
log.Fatal(http.ListenAndServe(":8080", handler))
}The core middleware uses the standard func(http.Handler) http.Handler signature. Framework-specific adapters are available as sub-packages:
| Framework | Package | Install |
|---|---|---|
| net/http, Chi | peekapi (core) |
go get github.com/peekapi-dev/sdk-go |
| Gin | peekapigin |
go get github.com/peekapi-dev/sdk-go/middleware/gin |
| Echo | peekapiecho |
go get github.com/peekapi-dev/sdk-go/middleware/echo |
| Fiber | peekapifiber |
go get github.com/peekapi-dev/sdk-go/middleware/fiber |
Chi uses the standard middleware signature natively — no extra package needed:
r := chi.NewRouter()
r.Use(peekapi.Middleware(client))import peekapigin "github.com/peekapi-dev/sdk-go/middleware/gin"
engine := gin.Default()
engine.Use(peekapigin.Middleware(client))import peekapiecho "github.com/peekapi-dev/sdk-go/middleware/echo"
e := echo.New()
e.Use(peekapiecho.Middleware(client))import peekapifiber "github.com/peekapi-dev/sdk-go/middleware/fiber"
app := fiber.New()
app.Use(peekapifiber.Middleware(client))
// With custom consumer identification
app.Use(peekapifiber.Middleware(client, peekapifiber.Options{
IdentifyConsumer: func(c *fiber.Ctx) string {
return c.Get("X-Tenant-ID")
},
}))| Field | Type | Default | Description |
|---|---|---|---|
APIKey |
string |
required | Your PeekAPI key |
Endpoint |
string |
PeekAPI cloud | Ingestion endpoint URL |
FlushInterval |
time.Duration |
10s |
Time between automatic flushes |
BatchSize |
int |
100 |
Events per batch (triggers flush) |
MaxBufferSize |
int |
10,000 |
Max events held in memory |
MaxStorageBytes |
int64 |
5MB |
Max disk fallback file size |
Debug |
bool |
false |
Enable debug logging |
IdentifyConsumer |
func(*http.Request) string |
auto | Custom consumer ID extraction |
StoragePath |
string |
temp dir | JSONL fallback file path |
TLSConfig |
*tls.Config |
nil |
Custom TLS configuration |
- Middleware intercepts every request/response
- Captures method, path, status code, response time, request/response sizes, consumer ID
- Events are buffered in memory and flushed in batches on a background goroutine
- On network failure: exponential backoff with jitter (1s, 2s, 4s, 8s, 16s)
- After max retries: events are persisted to a JSONL file on disk
- On next startup: persisted events are recovered and re-sent
- On SIGTERM/SIGINT: remaining buffer is flushed or persisted to disk
By default, consumers are identified by:
X-API-Keyheader — stored as-isAuthorizationheader — hashed with SHA-256 (stored ashash_<hex>)
Override with the IdentifyConsumer option:
client, _ := peekapi.New(peekapi.Options{
APIKey: "ak_live_xxx",
IdentifyConsumer: func(r *http.Request) string {
return r.Header.Get("X-Tenant-ID")
},
})ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := client.Shutdown(ctx); err != nil {
log.Printf("shutdown error: %v", err)
}- Zero dependencies — only Go stdlib
- Buffered batching — in-memory buffer with configurable flush interval and batch size
- Exponential backoff — retries with jitter on network failures
- Disk persistence — undelivered events saved to JSONL, recovered on restart
- SSRF protection — rejects private/loopback IP endpoints (except localhost)
- Consumer ID hashing — Authorization headers hashed with SHA-256
- Graceful shutdown — context-based with SIGTERM/SIGINT handlers
- Standard middleware —
func(http.Handler) http.Handlerworks with any router
Bug reports and feature requests: peekapi-dev/community
- Fork & clone the repo
- Run tests —
go test ./... - Lint —
go vet ./... - Format —
gofmt -w . - Submit a PR
If you find PeekAPI useful, give this repo a star — it helps others discover the project.
Show that your API is monitored by PeekAPI:
[](https://peekapi.dev)MIT