Go SDK
Install, configure, and use the Spare Go SDK.
The Go SDK requires Go 1.21+ and follows idiomatic Go error handling and context propagation.
Coming soon, under review
This SDK is under review and not yet published, so the package is not installable yet. The steps below show the intended usage. Until release, integrate over the REST API.
Installation
Install
go get <module-path>The exact module path (and its pkg.go.dev listing) is published when the Go SDK is released; <module-path> is a placeholder until then.
Configuration
Create a Configuration with your credentials, then build the client. Token exchange, caching, and refresh are handled automatically, you never manage tokens directly.
package main
import (
"context"
"log"
"os"
spareapi "<module-path>"
)
func main() {
ctx := context.Background()
config, err := spareapi.NewConfiguration(spareapi.ConfigurationOptions{
AppID: os.Getenv("SPARE_APP_ID"),
APIKey: os.Getenv("SPARE_API_KEY"),
Tenant: os.Getenv("SPARE_TENANT"),
})
if err != nil {
log.Fatal(err)
}
client := spareapi.NewClientFromConfiguration(config)
_ = client
}The X-Tenant header is injected on every request. Set SPARE_TENANT to the tenant for your market, see Coverage.
Common operations
// List providers
providers, err := client.Providers.List(ctx, &spareapi.ProvidersListRequest{
CountryCode: "AE",
})
if err != nil {
return err
}
for _, p := range providers.Data {
fmt.Println(p.Id, p.Name)
}
// Create a payment request, requires an ES256 detached-JWS signature
crypto := spareapi.NewCryptoHelper()
body := map[string]interface{}{
"type": "SingleInstantPayment",
"purpose": "ACM",
"creditorType": "MERCHANT",
"creditorReference": "INV-10042",
"creditorAccount": map[string]interface{}{
"identification": "AE070331234567890123456",
"name": "Acme Corp",
"schemeName": "IBAN",
},
"instructions": map[string]interface{}{
"amount": map[string]interface{}{
"amount": "250.00",
"currency": "AED",
},
},
}
xSignature, err := crypto.SignPayload(os.Getenv("SPARE_PRIVATE_KEY_PEM"), body)
if err != nil {
return err
}
paymentRequest, err := client.PaymentRequests.Create(ctx, &spareapi.CreatePaymentRequestRequest{
CreditorReference: "INV-10042",
Purpose: "ACM",
XSignature: xSignature,
})
if err != nil {
return err
}
fmt.Println("Payment request:", paymentRequest.Data.Id)Error handling
Inspect the returned error. For API errors, cast to *core.APIError for the status code and raw body:
import "<module-path>/core"
consent, err := client.PaymentConsents.Get(ctx, "consent-id")
if err != nil {
var apiErr *core.APIError
if errors.As(err, &apiErr) {
fmt.Println(apiErr.StatusCode) // HTTP status, e.g. 404
fmt.Println(apiErr.Body) // raw response body
}
return err
}Resources
- Quickstart
- API Reference
- Module path + source repository: published when the Go SDK is released.