Skip to content

kudrykv/go-monobank-api

Repository files navigation

Bindings for Monobank API

GoDoc CI Go Report Card codecov


go-monobank-api is the library to interact with the Monobank API. It provides clients for working with public and personal API.

One of its features is no dependencies on 3rd-party libraries.

The library allows to work with webhooks. It is possible to either parse the webhook response using the helper method, or receive webhooks in channel.

Usage

Basic usage

package main

import (
  "context"
  "fmt"

  mono "github.com/kudrykv/go-monobank-api"
)

func main() {
  public := mono.NewPublic()

  currencies, err := public.Currency(context.Background())
  if err != nil {
    panic(err)
  }

  fmt.Println(currencies)

  private := mono.NewPersonal("api-token")

  info, err := private.ClientInfo(context.Background())
  if err != nil {
    panic(err)
  }

  fmt.Println(info)
}

Webhooks

Example app for using channels:

package main

import (
  "context"
  "fmt"
  "net/http"

  mono "github.com/kudrykv/go-monobank-api"
)

func main() {
  personal := mono.NewPersonal("api-token")
  if err := personal.SetWebhook(context.Background(), "https://domain/webhook"); err != nil {
    panic(err)
  }

  webhookChan, handlerFunc := personal.ListenForWebhooks(context.Background())

  mux := http.NewServeMux()
  mux.HandleFunc("/webhook", handlerFunc)

  go func() {
    wh := <-webhookChan

    fmt.Println(wh)
  }()

  if err := http.ListenAndServe("", mux); err != nil {
    panic(err)
  }
}

Example app for using helper func:

package main

import (
  "context"
  "fmt"
  "net/http"

  mono "github.com/kudrykv/go-monobank-api"
)

func main() {
  personal := mono.NewPersonal("api-token")
  if err := personal.SetWebhook(context.Background(), "https://domain/webhook"); err != nil {
    panic(err)
  }

  mux := http.NewServeMux()
  mux.HandleFunc("/webhook", func(w http.ResponseWriter, r *http.Request) {
    webhook, err := personal.ParseWebhook(r.Context(), r.Body)
    if err != nil {
      w.WriteHeader(http.StatusInternalServerError)
      return
    }
    
    fmt.Println(webhook)
    w.WriteHeader(http.StatusOK)
  })

  if err := http.ListenAndServe("", mux); err != nil {
    panic(err)
  }
}

Support

Is something missing or works in unexpected way? Create for that an issue.

Monobank API Documentation

https://api.monobank.ua/docs/

You can obtain your personal token here.

Progress

  • Public API
  • Personal API
  • Corporate API (to do)