Skip to main content

Get started with the Go Modular Extend SDK

Last updated on January 23, 2025

Overview

The AccelByte Modular Extend SDK is provided to help developers invoke AccelByte Gaming Services (AGS) endpoints from within their backend code. The Extend SDK is generated from AGS OpenAPI specs and provides required functions such as login, refreshing tokens, validating tokens, and retrying HTTP requests. The AccelByte Modular Extend SDK is easy to use and enables developers to focus on what they want to do instead of spending time thinking on how to invoke endpoints.

The AccelByte Modular Extend SDK is available in multiple selected programming languages. We will show you the basics using the Go Modular Extend SDK.

Goals

This guide will show you how to:

  • Create a Go application project.
  • Add the Go Modular Extend SDK as a project dependency.
  • Use the Go Modular Extend SDK to invoke an AGS endpoint.
  • Run the application.

Prerequisites

To perform the tasks in this guide, you must have:

  • Installed Go 1.18 or later.
  • Gained access to the AGS demo environment:
    • Use https://prod.gamingservices.accelbyte.io for the AB_BASE_URL environment variable.
    • Follow the steps in the Create an OAuth client guide, using the Confidential client type. Use the Client ID and Client Secret for AB_CLIENT_ID and AB_CLIENT_SECRET environment variables respectively.

Create and run a Go application

  1. Create a Go application project.

    Create a folder and use go mod init to create a new Go application project.

    mkdir getting-started
    cd getting-started/
    go mod init golang-application
  2. Add the Go Modular Extend SDK as a project dependency.

    In this guide, we only use IAM services, therefore we only install the IAM Service Module into our project using the command below.

    go get github.com/AccelByte/accelbyte-go-modular-sdk/iam-sdk
    info

    If you're interested in using other AccelByte services, view our comprehensive list of available Go modules here.

  3. Use the Go Modular Extend SDK to invoke an AGS endpoint.

    To invoke an AGS endpoint using the Go Extend SDK, you must create a Go Extend SDK instance. Log in using Oauth Client credentials, and invoke the AGS endpoint GetCountryLocationV3. To do this, create a new main.go file with the following contents.

    package main

    import (
    "fmt"

    iam "github.com/AccelByte/accelbyte-go-modular-sdk/iam-sdk/pkg"
    "github.com/AccelByte/accelbyte-go-modular-sdk/iam-sdk/pkg/iamclient/o_auth2_0_extension"
    "github.com/AccelByte/accelbyte-go-modular-sdk/services-api/pkg/utils/auth"
    )

    var (
    // use the default config and token implementation
    configRepo = *auth.DefaultConfigRepositoryImpl()
    tokenRepo = *auth.DefaultTokenRepositoryImpl()
    )

    func main() {
    // prepare the IAM Oauth service
    oauth := &iam.OAuth20Service{
    Client: iam.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 {
    fmt.Println("failed login client")
    } else {
    fmt.Println("successful login")
    }

    // get the token
    token, _ := oauth.TokenRepository.GetToken()
    fmt.Printf("print %v\n", *token.AccessToken)

    // prepare the IAM's Oauth 2.0 Extension service
    oAuth20ExtensionService := &iam.OAuth20ExtensionService{
    Client: iam.NewIamClient(&configRepo),
    TokenRepository: &tokenRepo,
    }
    input := &o_auth2_0_extension.GetCountryLocationV3Params{}

    // call an AccelByte Gaming Services API e.g. GetCountryLocationV3
    ok, errLoc := oAuth20ExtensionService.GetCountryLocationV3Short(input)
    if errLoc != nil {
    fmt.Println(errLoc.Error())
    } else {
    fmt.Printf("Country name: %s\n", *ok.CountryName)
    }
    }

    The ConfigRepositoryImpl gets its values from AB_BASE_URL, AB_CLIENT_ID, and AB_CLIENT_SECRET environment variables.

  4. Run the application.

    Set the required environment variables, then run the application using go run main.go.

    $ export AB_BASE_URL="<your environment's domain URL>"         # AccelByte Cloud Base URL e.g. demo environment
    $ export AB_CLIENT_ID="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Put AccelByte Cloud OAuth Client ID
    $ export AB_CLIENT_SECRET="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Put AccelByte Cloud OAuth Client Secret
    $ go run main.go

Next steps

  • As a next step, you can consider adding the Go Modular Extend SDK as a project dependency and using it in your own project.

Resources

  • For more advanced usage of Go Modular Extend SDK, refer to the AccelByte Go Modular SDK guide.
  • For examples of common use cases using the Go Modular Extend SDK, refer to the Common use cases list.
  • If you know which AGS endpoints you need to invoke and you want to invoke them using the Go Modular Extend SDK, refer to the Operations docs.