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

# Python SDK Getting Started Guide

# Overview

You can use AccelByte Cloud’s Python 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 Python 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.
  • Python 3.9
  • Python IDE e.g., Visual Studio Code

# Additional Resources

# Tutorial

# Create a Python Project

  1. Create a folder and use venv to create a Python virtual environment.

# Add to Project Dependency

  1. Install SDK dependencies.
$ pip install requests httpx websockets pyyaml
  1. Install the SDK.
$ pip install git+https://github.com/AccelByte/accelbyte-python-sdk.git@{VERSION}#egg=accelbyte_py_sdk
  1. Replace {VERSION} with a specific release version tag from releases (opens new window).

NOTE

We recommended using the Python Server SDK version that matches your AccelByte Cloud version.

# Use in Code

  1. Create an SDK instance, log in using client credentials, and call an AccelByte Cloud API in app.py.
  2. The DefaultConfigRepository gets its values from AB_BASE_URL, AB_CLIENT_ID, and AB_CLIENT_SECRET environment variables.
# app.py

import accelbyte_py_sdk
from accelbyte_py_sdk.core import (
    RequestsHttpClient,
    EnvironmentConfigRepository,
    InMemoryTokenRepository,
)
import accelbyte_py_sdk.services.auth as auth_service
import accelbyte_py_sdk.api.iam as iam_service


def main():
    # Create default HTTP client, token repository, and config repository instances
    http_client = RequestsHttpClient()
    config_repository = EnvironmentConfigRepository()
    token_repository = InMemoryTokenRepository()

    # Initialize the SDK
    accelbyte_py_sdk.initialize(
        options={
            "config": config_repository,
            "token": token_repository,
            "http": http_client,
        }
    )

    # Login using client credentials
    token, error = auth_service.login_client()
    if error:
        exit(1)  # Login failed

    # Call an AccelByte Cloud API e.g. GetCountryLocationV3
    response, error = iam_service.get_country_location_v3()
    if error:
        exit(1)  # Response error

    print(response.country_name)


if __name__ == "__main__":
    main()

# Run the Code

  1. Set the required environment variables and run the code using the Python interpreter.
$ 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

$ python app.py

# Import AccelByte Services

Now you can start using any of the following AccelByte services in your application, by importing the following syntax to your code. See the model for each service below or in the how-to (opens new window) folder inside the accelbyte-python-sdk (opens new window) repository.

# SDK Examples

Now you can start using any of the following AccelByte Cloud services in your application, by importing the following syntax to your code. See the model for each service below or in the how-to (opens new window) folder inside the accelbyte-python-sdk (opens new window) repository.

IAM

API Docs (opens new window)

SDK reference (opens new window)

from accel_py_sdk.api.iam import *
from accelbyte_py_sdk.services.auth import *
Basic

API Docs (opens new window)

SDK reference (opens new window)

from accel_py_sdk.api.basic import *
Social

API Docs (opens new window)

SDK reference (opens new window)

from accel_py_sdk.api.social import *
Platform

API Docs (opens new window)

SDK reference (opens new window)

from accel_py_sdk.api.platform import *
Group

API Docs (opens new window)

SDK reference (opens new window)

from accel_py_sdk.api.group import *
Cloud Save

API Docs (opens new window)

SDK reference (opens new window)

from accel_py_sdk.api.cloudsave import *
DSM Controller

API Docs (opens new window)

SDK reference (opens new window)

from accel_py_sdk.api.dsm_controller import *
Session Browser

API Docs (opens new window)

SDK reference (opens new window)

from accel_py_sdk.api.session_browser import *
Lobby

API Docs (opens new window)

SDK reference (opens new window)

from accel_py_sdk.api.lobby import *
Telemetry

API Docs (opens new window)

SDK reference (opens new window)

from accel_py_sdk.api.amalgam_game_telemetry import *