RespKit

Quick start

Requirements

The core RespKit module supports Go 1.22 and later and has no third-party runtime dependencies. Framework test suites and optional adapters use separate Go modules.

The module path for this source is:

github.com/ZihxS/RespKit

Install the response package:

go get github.com/ZihxS/RespKit/resp

Your first response with net/http

package main

import (
	"log"
	"net/http"

	"github.com/ZihxS/RespKit/resp"
)

func main() {
	http.HandleFunc("/users/123", func(w http.ResponseWriter, r *http.Request) {
		user := map[string]any{
			"user_id": 123,
			"name":    "Ada",
		}
		if err := resp.OK(resp.HTTP(w, r), user); err != nil {
			log.Printf("write response: %v", err)
		}
	})

	log.Fatal(http.ListenAndServe(":8080", nil))
}

The response uses status 200 and the default envelope:

{
  "success": true,
  "data": {
    "user_id": 123,
    "name": "Ada"
  }
}

resp.HTTP combines an http.ResponseWriter and *http.Request into the context expected by RespKit. The same pattern works with Chi and Gorilla/mux.

Return an error

Use a domain error rather than a raw database or provider message:

if err := resp.Fail(ctx, resp.ErrNotFound); err != nil {
	log.Printf("write response: %v", err)
}

RespKit selects the status and public error code. The raw internal error remains on the server and is not included in the response by default.

Use RespKit without configuration

Package-level helpers use the default engine, so a basic handler needs no setup. To use separate policies for different APIs, create an engine with resp.New. See configuration for the available options.

On this page