Last Updated: 8/19/2022, 10:39:25 PM

# Golang SDK Getting Started Guide

# Overview

You can use AccelByte Cloud’s Golang SDK to implement our backend services within your game. The SDK acts as a bridge between your game and our services. This guide will show you how to create an application that uses Golang Server SDK from scratch.

# Prerequisites

  • AccelByte Cloud (demo environment):
    • Use https://demo.accelbyte.io (opens new window) for AB_BASE_URL environment variable.
    • Create an Oauth Client with client type Confidential.
    • Use Client ID value for AB_CLIENT_ID environment variable.
    • Use Client Secret value for AB_CLIENT_SECRET environment variable.
  • Golang 1.16 or newer
  • Golang IDE, e.g., Goland, Visual Studio Code

# Additional Resources

# Tutorial

# Create a Golang Project

  1. Create a folder and use go mod init to create a Golang project.
$ mkdir myproject
$ cd myproject/
$ go mod init golang-application
  1. Add the required configuration in go.mod.
  2. Replace {VERSION} with a specific release version tag from releases and run go mod tidy. We recommended using the Golang Server SDK version that matches your AccelByte Cloud version.
module golang-application

go 1.16

require (
    github.com/AccelByte/accelbyte-go-sdk {VERSION}
)

# Use in Code

  1. Create an SDK instance, log in using client credentials, and call an AccelByte Cloud API in main.go.
  2. Create the file main.go inside the project directory. The ConfigRepositoryImpl gets its values from AB_BASE_URL, AB_CLIENT_ID, and AB_CLIENT_SECRET environment variables.
package main

import (
    "fmt"
    "os"
    "time"

    "github.com/AccelByte/accelbyte-go-sdk/iam-sdk/pkg/iamclient/o_auth2_0_extension"
    "github.com/AccelByte/accelbyte-go-sdk/iam-sdk/pkg/iamclientmodels"
    "github.com/sirupsen/logrus"

    "github.com/AccelByte/accelbyte-go-sdk/services-api/pkg/factory"
    "github.com/AccelByte/accelbyte-go-sdk/services-api/pkg/service/iam"
)

func main() {
    // prepare the IAM Oauth service
    oauth := &iam.OAuth20Service{
        Client:           factory.NewIamClient(&configRepo),
        ConfigRepository: &configRepo,
        TokenRepository:  &tokenRepo,
    }
    clientId := oauth.ConfigRepository.GetClientId()
    clientSecret := oauth.ConfigRepository.GetClientSecret()

    // call the endpoint tokenGrantV3Short through the wrapper 'LoginClient'
    err := oauth.LoginClient(&clientId, &clientSecret)
    if err != nil {
        logrus.Error("failed login client")
    } else {
        logrus.Info("successful login")
    }

    // get the token
    token, _ := oauth.TokenRepository.GetToken()
    logrus.Infof("print %v", *token.AccessToken)

    // call an AccelByte Cloud API e.g. GetCountryLocationV3
    oAuth20ExtensionService := &iam.OAuth20ExtensionService{
        Client:           factory.NewIamClient(&configRepo),
        TokenRepository:  &tokenRepo,
    }
    input := &o_auth2_0_extension.GetCountryLocationV3Params{}
    ok, _ := oAuth20ExtensionService.GetCountryLocationV3Short(input)
    if err != nil {
        logrus.Error(err.Error())
    } else {
        logrus.Infof("Country name: %s", *ok.CountryName)
    }
}

/*
This TokenRepositoryImpl and ConfigRepositoryImpl is a mandatory implementation
before using the SDK. The implementation inside and naming can be adjusted accordingly
*/
type TokenRepositoryImpl struct {
    IssuedTime  *time.Time
    accessToken *iamclientmodels.OauthmodelTokenResponseV3
}

type ConfigRepositoryImpl struct {
    baseUrl      string
    clientId     string
    clientSecret string
}

var (
    tokenRepo = TokenRepositoryImpl{
        accessToken: &iamclientmodels.OauthmodelTokenResponseV3{},
    }
    configRepo = ConfigRepositoryImpl{
        baseUrl:      os.Getenv("AB_BASE_URL"),
        clientId:     os.Getenv("AB_CLIENT_ID"),
        clientSecret: os.Getenv("AB_CLIENT_SECRET"),
    }
)

func (t *TokenRepositoryImpl) Store(accessToken iamclientmodels.OauthmodelTokenResponseV3) error {
    timeNow := time.Now().UTC()
    t.IssuedTime = &timeNow
    t.accessToken = &accessToken

    return nil
}

func (t *TokenRepositoryImpl) GetToken() (*iamclientmodels.OauthmodelTokenResponseV3, error) {
    if t.accessToken == nil {
        return nil, fmt.Errorf("empty access Token")
    }

    return t.accessToken, nil
}

func (t *TokenRepositoryImpl) RemoveToken() error {
    t.IssuedTime = nil
    t.accessToken = nil

    return nil
}

func (t *TokenRepositoryImpl) TokenIssuedTimeUTC() time.Time {
    return *t.IssuedTime
}

func (c *ConfigRepositoryImpl) GetClientId() string {
    return c.clientId
}

func (c *ConfigRepositoryImpl) GetClientSecret() string {
    return c.clientSecret
}

func (c *ConfigRepositoryImpl) GetJusticeBaseUrl() string {
    return c.baseUrl
}

# Run the Code

  1. Set the required environment variables and run the code using go run main.go.
  • The TokenRepositoryImpl struct to get the Client ID and Client Secret.
$ export AB_BASE_URL="https://demo.accelbyte.io"              # AccelByte Cloud Base URL e.g. demo environment
$ export AB_CLIENT_ID="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"      # AccelByte Cloud OAuth Client ID
$ export AB_CLIENT_SECRET="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"  # AccelByte Cloud OAuth Client Secret
$ go run main.go

# Import AccelByte Services

Now you’re ready to integrate any of the following AccelByte services into your application. All services have already been imported in .../pkg/service.<your-service> will be changed to the service name you want to use automatically during the import.

# SDK Examples

See our Golang SDK example repo (opens new window) for a selection of test cases you can use to customize your game.

IAM

API Docs (opens new window)

SDK Reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/iam-sdk/pkg/iamclientmodels"
)
Basic

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/basic-sdk/pkg/basicclientmodels"
)
Social

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/social-sdk/pkg/socialclientmodels"
)
Platform

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/platform-sdk/pkg/platformclientmodels"
)
Group

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/group-sdk/pkg/groupclientmodels"
)
Cloud Save

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/cloudsave-sdk/pkg/cloudsaveclientmodels"
)
DSM Controller

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/dsmc-sdk/pkg/dsmcclientmodels"
)
Session Browser

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/sessionbrowser-sdk/pkg/sessionbrowserclientmodels"
)
Lobby

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/lobby-sdk/pkg/lobbyclientmodels"
)
Achievement

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/achievement-sdk/pkg/achievementclientmodels"
)
DS Log Manager

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/dslogmanager-sdk/pkg/dslogmanagerclientmodels"
)
UGC

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/ugc-sdk/pkg/ugcclientmodels"
)
Leaderboard

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/leaderboard-sdk/pkg/leaderboardclientmodels"
)
GDPR

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/gdpr-sdk/pkg/gdprclientmodels"
)
Legal

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/legal-sdk/pkg/legalclientmodels"
)
Matchmaking

API Docs (opens new window)

SDK reference (opens new window)

import (
 "github.com/AccelByte/accelbyte-go-sdk/matchmaking-sdk/pkg/matchmakingclientmodels"
)