Base Configuration
Configure the base settings for your API client
@BaseURL
Sets the base URL for all API endpoints. Must be set at the interface level.
// @BaseURL("https://api.example.com/v1")
type ExampleAPI interface {
genapi.Interface
// Methods...
}
HTTP Methods
Define HTTP method and path for endpoints
@GET, @POST, @PUT, @DELETE, @PATCH
Specify the HTTP method and path for an API endpoint.
// @GET("/users")
GetUsers(ctx context.Context) ([]User, error)
// @POST("/users")
CreateUser(ctx context.Context, user User) (User, error)
// @PUT("/users/{id}")
UpdateUser(ctx context.Context, id string, user User) (User, error)
// @DELETE("/users/{id}")
DeleteUser(ctx context.Context, id string) error
// @PATCH("/users/{id}/status")
UpdateUserStatus(ctx context.Context, id string, status UserStatus) error
Parameters
Working with URL parameters, query parameters, and paths
Path Parameters
Parameters enclosed in curly braces in the URL path are automatically mapped to method arguments with the same name.
// @GET("/users/{id}/posts/{postId}")
GetUserPost(ctx context.Context, id string, postId int) (Post, error)
@Query
Use @Query to define query parameters for an endpoint.
// @GET("/users")
// @Query("page", "limit", "sort")
GetUsers(ctx context.Context, page int, limit int, sort string) ([]User, error)
Request Body
Handling request payloads and serialization
Automatic Body Serialization
Complex types after the context parameter are automatically serialized as JSON request bodies.
// @POST("/users")
CreateUser(ctx context.Context, user User) (User, error)
type User struct {
ID string `json:"id,omitempty"`
Name string `json:"name"`
Email string `json:"email"`
CreatedAt string `json:"created_at,omitempty"`
}
Response Handling
Processing API responses and handling errors
Automatic Response Deserialization
Return types are automatically deserialized from JSON API responses.
// No response body
func DeleteUser(ctx context.Context, id string) error
// API returns a User object as JSON, automatically deserialized to the User struct
// Typed response with error handling
func GetUser(ctx context.Context, id string) (User, error)
// You can specify the raw genapi.Response as well
// Raw response access
func GetRawResponse(ctx context.Context) (*genapi.Response, error)
// Must-style response (panics on error)
func MustGetUser(ctx context.Context, id string) User
Headers & Auth
Working with HTTP headers and authentication
@Header
Define custom HTTP headers for requests.
// Header("User-Agent", "genapi")
type SomeAPI interface {
genapi.Interface
// @GET("/special-endpoint")
// @Header("X-Custom-Header: custom-value")
// @Header("X-Api-Version: 2")
GetSpecialData(ctx context.Context) (SpecialData, error)
}