Go Library Quickstart

Configure Project

  1. First add the library module to your project.
go get github.com/dapi-co/dapi-go
  1. Create a Dapi app and products instances with your App Secret.
package main

import (
    "github.com/dapi-co/dapi-go/app"
    "github.com/dapi-co/dapi-go/auth"
    "github.com/dapi-co/dapi-go/config"
    "github.com/dapi-co/dapi-go/data"
    "github.com/dapi-co/dapi-go/metadata"
    "github.com/dapi-co/dapi-go/payment"
)

func main() {
    // create a config object that holds the secret of this app
    myAppConfig := &config.Config{
        AppSecret: "YOUR_APP_SECRET",
    }

    // init a DapiApp instance
    myApp := &app.DapiApp{
        Config: myAppConfig,
    }

    // init the products you want to use
    myAuth := auth.Auth{Config: myAppConfig}
    myData := data.Data{Config: myAppConfig}
    myPayment := payment.Payment{Config: myAppConfig}
    myMetadata := metadata.Metadata{Config: myAppConfig}

    // use any of the myApp, myAuth, myData, myPayment, or myMetadata methods..
}
  1. Now you can use any of the functions of the DapiApp instance, myApp. Here is an example for GetAccounts
package main

import (
    "github.com/dapi-co/dapi-go/app"
    "github.com/dapi-co/dapi-go/config"
    "github.com/dapi-co/dapi-go/data"
)

func main() {
    // create a config object that holds the secret of this app
    myAppConfig := &config.Config{
        AppSecret: "YOUR_APP_SECRET",
    }

    // init the product you want to use
    myData := data.Data{Config: myAppConfig}

    // use any of the `myData` methods..

    // provide the operationID and the userInputs only if needed
    accountsResp, err := myData.GetAccounts("YOUR_ACCESS_TOKEN", "YOUR_USER_SECRET", nil, "")
    if err != nil {
        // handle the error..
        return
    }
    if accountsResp.Status != constants.StatusDone {
        // handle the unsuccessful response..
        return
    }

    accounts := accountsResp.Accounts
    // use the got accounts array..
}

Configure SDK Server

  1. Follow the steps in Configure Project first in order to obtain and import the library.

  2. You can use the DapiApp instance to handle requests to a specific endpoint in your server. Our code will basically update the request to add your app's appSecret to it, and forward the request to Dapi, then respond back with the got response.

package main

import (
    "log"
    "net/http"

    "github.com/dapi-co/dapi-go/app"
    "github.com/dapi-co/dapi-go/config"
)

func main() {
    // create a config object that holds the secret of this app
    myAppConfig := &config.Config{
        AppSecret: "YOUR_APP_SECRET",
    }

    // init the a DapiApp instance
    myApp := &app.DapiApp{
        Config: myAppConfig,
    }

    // start a simple server that listens on the provided endpoint, and handles requests
    // through the handler of the DapiApp instance.
    err := http.ListenAndServe("YOUR_ADDRESS", http.HandlerFunc(myApp.HandleSDKDapiRequests))
    if err != nil {
        log.Fatal(err)
    }
}