bunqr

package module
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 30, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

README

bunqr

This is a go SDK for the Bunq API. The basics are there but is not a finished product - use at your own risk. There are no dependencies.

It contains a package for the OAuth2 authentication flow, based on the go http library, that can also be used independently.

The SDK.Client struct exposes all API endpoints of the Bunq API. Some of them will not work out of the box! See below for how to fix. It's because the code is generated from the API spec. Is this the best approach? In hindsight, probably not. But fixes are usually not difficult to make.

Try it out

Review the Bunq API documentation.

  1. In the Bunq app, go to settings -> OAuth (under Developers). Fill in the callback URL (for the example it's http://localhost:8080/callback) and copy the client id and secret to your PC.
  2. export BUNQ_CLIENT_ID=<client id>
    export BUNQ_CLIENT_SECRET=<secret>
    
  3. cd example
  4. Generate a private key: openssl genrsa -out private_key.pem 2048.
  5. go run .
  6. Open http://localhost:8080 in your browser.

Using the SDK

Take a look at the example for the example code. This code is not suitable for production as it is, but it gives a good idea of what you need to implement. You can use it with an API key or OAuth2 (recommended).

Fixing the API

The code in the client package that calls the APIs is generated based on a tweaked version of the official OpenAPI definition. The definition is notoriously bad. That means there's a fair chance that if you try a function, it doesn't work right away and you have to fix the definition first to match the request and response, and regenerate the code.

Open client/schema_fixed.json and make your changes. It can help to store the response body as a json in client/testdata and write a test. Don't commit personal information.

Run go mod tidy once to download the oapi-codegen tool. Then execute:

cd client
go generate ./...

sed -i.bak -e 's/\*string/string/g' api.gen.go
sed -i.bak -e 's/\*int/int/g' api.gen.go
sed -i.bak -e 's/\*bool/bool/g' api.gen.go
sed -i.bak -e 's/\*\[\]/\[\]/g' api.gen.go
sed -i.bak -e 's/\*Amount/Amount/g' api.gen.go
sed -i.bak -e 's/CounterpartyAlias   \*/CounterpartyAlias   /g' api.gen.go
rm api.gen.go.bak

Contribute

PRs with tests are welcome.

Documentation

Index

Constants

View Source
const (
	PROD_URL    = "https://api.bunq.com/v1"
	SANDBOX_URL = "https://public-api.sandbox.bunq.com/v1/"
)

Variables

This section is empty.

Functions

func ReadKey

func ReadKey(keyPath string) (*rsa.PrivateKey, error)

ReadKey is a helper function to convert PKCS8 RSA key.

Types

type FilePersistence

type FilePersistence struct {
	// contains filtered or unexported fields
}

FilePersistence stores an Installation as a file.

func NewFilePersistence

func NewFilePersistence(filename string) FilePersistence

func (FilePersistence) GetInstallation

func (f FilePersistence) GetInstallation() (*Installation, error)

func (FilePersistence) StoreInstallation

func (f FilePersistence) StoreInstallation(ins Installation) error

type Installation

type Installation struct {
	ServerKey         string
	InstallationToken string
}

Installation is the metadata of this deployment.

type Persistence

type Persistence interface {
	StoreInstallation(Installation) error
	GetInstallation() (*Installation, error)
}

Persistence stores the Installation for bunq. It should only be created once.

type SDK added in v0.1.1

type SDK struct {
	sync.RWMutex
	Client *client.Client
	// contains filtered or unexported fields
}

SDK is a bunq SDK. Access the API through the Client object.

func New

func New(apiURL string, persistence Persistence, key *rsa.PrivateKey, logger *slog.Logger) (*SDK, error)

New creates a new bunq SDK. It cannot be used until Init is called.

func (*SDK) CreateDevice added in v0.1.1

func (b *SDK) CreateDevice(ctx context.Context, authToken string, permittedIPs []string) error

CreateDevice must be called once for a deployment. The authToken can be retrieved from a successful OAuth login or by passing the API key directly. PermittedIPs is an optional list of IP addresses that may access the API with this token. It defaults to the current outgoing IP. It accepts wildcards.

func (*SDK) CreateSession added in v0.1.1

func (b *SDK) CreateSession(ctx context.Context, userToken string) (sv Session, err error)

CreateSession creates a session from an existing token. The values from this session can be used to call the API.

func (*SDK) Init added in v0.1.1

func (b *SDK) Init() error

Init must be called before using the SDK. It calls the API to create an Installation if it's not there yet.

type Session

type Session struct {
	// AccessToken is the token that grants access to the API.
	AccessToken string

	// APIKeyID should be used in place of the userID for API endpoints if using OAuth2.
	APIKeyID int

	// The UserID is the id of the logged in user (if using OAuth2) or the API key owner.
	UserID int

	// The Nickname is the nickname of the logged in user (if using OAuth2) or the API key owner.
	Nickname string

	// Expires is the expiry time of this session.
	Expires time.Time
}

Session contains the most important info from a Bunq API session.

Directories

Path Synopsis
Package client provides primitives to interact with the openapi HTTP API.
Package client provides primitives to interact with the openapi HTTP API.
oauth provides http handlers for the bunq oauth flow.
oauth provides http handlers for the bunq oauth flow.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL