Quickstart

Quickstart: call the Credicorp public API from Go

Go's standard library is all you need to call the Credicorp public API. This quickstart uses net/http and encoding/json to list products into a typed struct, with a context timeout and decoding of the documented error envelope — no third-party HTTP client required.

2 min read

stdlibnet/http + encoding/json
contextTimeout via context
typedDecode into structs

List products

Define a struct that matches the envelope, then decode into it:

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"net/http"
	"time"
)

type Product struct {
	ID        string  `json:"id"`
	Name      string  `json:"name"`
	MaxAmount float64 `json:"max_amount"`
}
type ListResp struct {
	Data  []Product `json:"data"`
	Error *struct {
		Code    string `json:"code"`
		Message string `json:"message"`
	} `json:"error"`
}

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	req, _ := http.NewRequestWithContext(ctx, "GET",
		"https://api.credicorp.co.uk/public/v1/products", nil)
	req.Header.Set("Accept", "application/json")
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer res.Body.Close()
	var out ListResp
	json.NewDecoder(res.Body).Decode(&out)
	if out.Error != nil {
		panic(fmt.Sprintf("%s: %s", out.Error.Code, out.Error.Message))
	}
	for _, p := range out.Data {
		fmt.Printf("%s \u2014 up to \u00a3%.0f\n", p.Name, p.MaxAmount)
	}
}

Reuse the client

Create one http.Client with a timeout at package scope and reuse it — never call http.Get in a loop, which leaks connections. The context deadline above cancels a slow request cleanly.

Frequently asked questions

Do I need a third-party HTTP library?

No. net/http covers every public-ring call. Add a router or client library only if your wider project already uses one.

How should I model money fields?

Amounts are returned as JSON numbers in whole pounds for these products. If you need exact decimal arithmetic, decode into a string or a decimal type rather than float64.

Funding for UK limited companies

Credicorp lends to your company, not to you personally — short-term working capital with no personal guarantee. See what your business could access.