SDKs

Go

Requirements

This module requires Go version >= 1.13.

Installation

Run the following command to use the Mercoa Go library in your module:

1go get github.com/mercoa-finance/go

Usage

1import (
2 mercoa "github.com/mercoa-finance/go"
3 mercoaclient "github.com/mercoa-finance/go/client"
4 "github.com/mercoa-finance/go/option"
5)
6
7client := mercoaclient.NewClient(
8 option.WithToken("<YOUR_API_KEY>"),
9)
10
11response, err := client.Fees.Calculate(
12 context.TODO(),
13 &mercoa.CalculateFeesRequest{
14 Amount: 42.0,
15 PaymentSourceID: "pm_c0f9f5e8-516b-4516-9185-0a2c67ed1fe5",
16 PaymentDestinationID: "pm_12121928-47a0-488b-9357-70e1fded0568",
17
18 },
19)

Optionals

This library models optional primitives and enum types as pointers. This is primarily meant to distinguish default zero values from explicit values (e.g. false for bool and "" for string). A collection of helper functions are provided to easily map a primitive or enum to its pointer-equivalent (e.g. mercoa.Int).

For example, consider the client.Entity.Find endpoint usage below:

1response, err := client.Entity.Find(
2 context.TODO(),
3 &entity.FindEntities{
4 IsCustomer: mercoa.Bool(true),
5 Limit: mercoa.Int(100),
6 Status: []*mercoa.EntityStatus{
7 mercoa.EntityStatusVerified.Ptr(),
8 },
9 },
10)

Timeouts

Setting a timeout for each individual request is as simple as using the standard context library. Setting a one second timeout for an individual API call looks like the following:

1ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
2defer cancel()
3
4response, err := client.Fees.Calculate(
5 ctx,
6 &mercoa.CalculateFeesRequest{
7 Amount: 42.0,
8 PaymentSourceID: "pm_c0f9f5e8-516b-4516-9185-0a2c67ed1fe5",
9 PaymentDestinationID: "pm_12121928-47a0-488b-9357-70e1fded0568",
10
11 },
12)

Request Options

A variety of request options are included to adapt the behavior of the library, which includes configuring authorization tokens, or providing your own instrumented *http.Client. Both of these options are shown below:

1client := mercoaclient.NewClient(
2 option.WithToken("<YOUR_API_KEY>"),
3 option.WithHTTPClient(
4 &http.Client{
5 Timeout: 5 * time.Second,
6 },
7 ),
8)

These request options can either be specified on the client so that they’re applied on every request (shown above), or for an individual request like so:

1response, err := client.Fees.Calculate(
2 ctx,
3 &mercoa.CalculateFeesRequest{
4 Amount: 42.0,
5 PaymentSourceID: "pm_c0f9f5e8-516b-4516-9185-0a2c67ed1fe5",
6 PaymentDestinationID: "pm_12121928-47a0-488b-9357-70e1fded0568",
7 },
8 option.WithToken("<YOUR_API_KEY>"),
9)

Providing your own *http.Client is recommended. Otherwise, the http.DefaultClient will be used, and your client will wait indefinitely for a response (unless the per-request, context-based timeout is used).

Automatic Retries

The Mercoa Go client is instrumented with automatic retries with exponential backoff. A request will be retried as long as the request is deemed retriable and the number of retry attempts has not grown larger than the configured retry limit (default: 2).

A request is deemed retriable when any of the following HTTP status codes is returned:

  • 408 (Timeout)
  • 409 (Conflict)
  • 429 (Too Many Requests)
  • 5XX (Internal Server Errors)

You can use the option.WithMaxAttempts option to configure the maximum retry limit to your liking. For example, if you want to disable retries for the client entirely, you can set this value to 1 like so:

1client := mercoaclient.NewClient(
2 option.WithMaxAttempts(1),
3)

This can be done for an individual request, too:

1response, err := client.Fees.Calculate(
2 ctx,
3 &mercoa.CalculateFeesRequest{
4 Amount: 42.0,
5 PaymentSourceID: "pm_c0f9f5e8-516b-4516-9185-0a2c67ed1fe5",
6 PaymentDestinationID: "pm_12121928-47a0-488b-9357-70e1fded0568",
7 },
8 option.WithMaxAttempts(1),
9)

Errors

Structured error types are returned from API calls that return non-success status codes. For example, you can check if the error was due to a bad request (i.e. status code 400) with the following:

1response, err := client.Fees.Calculate(
2 ctx,
3 &mercoa.CalculateFeesRequest{
4 Amount: 42.0,
5 PaymentSourceID: "pm_c0f9f5e8-516b-4516-9185-0a2c67ed1fe5",
6 PaymentDestinationID: "pm_12121928-47a0-488b-9357-70e1fded0568",
7 },
8)
9if err != nil {
10 if notFoundErr, ok := err.(*mercoa.NotFound);
11 // Do something with the not found error ...
12 }
13 return err
14}

These errors are also compatible with the errors.Is and errors.As APIs, so you can access the error like so:

1response, err := client.Fees.Calculate(
2 ctx,
3 &mercoa.CalculateFeesRequest{
4 Amount: 42.0,
5 PaymentSourceID: "pm_c0f9f5e8-516b-4516-9185-0a2c67ed1fe5",
6 PaymentDestinationID: "pm_12121928-47a0-488b-9357-70e1fded0568",
7 },
8)
9if err != nil {
10 var notFoundErr *mercoa.NotFound
11 if errors.As(err, notFoundErr) {
12 // Do something with the not found error ...
13 }
14 return err
15}

If you’d like to wrap the errors with additional information and still retain the ability to access the type with errors.Is and errors.As, you can use the %w directive:

1response, err := client.Fees.Calculate(
2 ctx,
3 &mercoa.CalculateFeesRequest{
4 Amount: 42.0,
5 PaymentSourceID: "pm_c0f9f5e8-516b-4516-9185-0a2c67ed1fe5",
6 PaymentDestinationID: "pm_12121928-47a0-488b-9357-70e1fded0568",
7 },
8)
9if err != nil {
10 return fmt.Errorf("failed to calculate fees: %w", err)
11}