Last Updated: 11/4/2021, 3:15:17 AM

# Python SDK Getting Started Guide

# Overview

You can use the AccelByte Python SDK to implement our backend services within your game. The SDK acts as a bridge between your game and our services. Follow the tutorials below to learn how to set up our Python SDK.

# Prerequisites

# Additional Resources

# Tutorials

# Install the AccelByte Python SDK

You can install the SDK with pip directly from the GitHub repository, as seen below:

pip install git+ssh://git@github.com/AccelByte/accelbyte-python-sdk.git@master#egg=accelbyte_py_sdk

# Log in as a Client using the SDK

To log in as the client you created earlier, import the accelbyte_py_sdk, MyConfigRepository, and list the client’s Base URL, Client ID, Client Secret, and Namespace.

Next, initialize the python SDK and call token_grant_v3 with the grant type set as "client_credentials".

import accelbyte_py_sdk
from accelbyte_py_sdk.core import MyConfigRepository
from accelbyte_py_sdk.api.iam import token_grant_v3

config = MyConfigRepository(
    base_url="<your-base-url>",
    client_id="<your-client-id>",
    client_secret="<your-client-secret>",
    namespace="<your-namespace>",
)

accelbyte_py_sdk.initialize(
    options={
        "config":config,
    },
)

_, error = token_grant_v3(grant_type="client_credentials")
if error:
    print(error)

NOTE

You can also paste in configuration by replacing MyConfigRepository(...) with the following value.

  • If your configuration data is in a JSON file, replace it with JsonFileConfigRepository("path/to/the/config/file.json")
  • If your configuration data is set as Environment Variables (AB_BASE_URL, AB_CLIENT_ID, AB_CLIENT_SECRET, AB_NAMESPACE, etc.), replace it with EnvironmentConfigRepository(...)

# 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:

IAM

API Docs (opens new window)

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

API Docs (opens new window)

from accel_py_sdk.api.basic import *
Social

API Docs (opens new window)

from accel_py_sdk.api.social import *
Platform

API Docs (opens new window)

from accel_py_sdk.api.platform import *
Group

API Docs (opens new window)

from accel_py_sdk.api.group import *
Cloud Save

API Docs (opens new window)

from accel_py_sdk.api.cloudsave import *
DSM Controller

API Docs (opens new window)

from accel_py_sdk.api.dsm_controller import *
Session Browser

API Docs (opens new window)

from accel_py_sdk.api.session_browser import *
Lobby

API Docs (opens new window)

from accel_py_sdk.api.lobby import *
Telemetry

API Docs (opens new window)

from accel_py_sdk.api.amalgam_game_telemetry import *

# Implement the Statistics Service using the SDK

In this section, you’ll learn how to implement our Statistics service using the Python SDK. Make sure the client you created earlier has the necessary permissions to access the Statistics service. After that, you can use the SDK to create, read, update, delete statistics for a player from your game client.

# Create a Statistic Configuration

POST /social/v1/admin/namespaces/{namespace}/stats

from accelbyte_py_sdk.api.social import create_stat
from accelbyte_py_sdk.api.social.models import StatCreate


result, error = create_stat(
    body=StatCreate.create(
        stat_code="<your-stat-code>",
        name="<your-name>",
        default_value=1,
        set_by="CLIENT",
    ),
)
if error:
    print(error)

# Delete a Statistic Configuration

DELETE /social/v1/admin/namespaces/{namespace}/stats/{statCode}

from accelbyte_py_sdk.api.social import delete_stat


result, error = delete_stat(
    stat_code="<your-stat-code>",
)
if error:
    print(error)

# Retrieve a Statistic Configuration by Stat Code

GET /social/v1/admin/namespaces/{namespace}/stats/{statCode}

from accelbyte_py_sdk.api.social import get_stat


result, error = get_stat(
    stat_code="<your-stat-code>",
)
if error:
    print(error)

# Retrieve All Statistic Configurations

GET /social/v1/admin/namespaces/{namespace}/stats

from accelbyte_py_sdk.api.social import get_stats

result, error = get_stats(
    offset=0,
    limit=20,
)
if error:
    print(error)

# Search for a Statistic Configuration by Keyword

GET /social/v1/admin/namespaces/{namespace}/stats/search

from accelbyte_py_sdk.api.social import query_stats

result, error = query_stats(
    keyword="<your-keyword>",
    offset=0,
    limit=20,
)
if error:
    print(error)

# Update a Statistic Configuration

PATCH /social/v1/admin/namespaces/{namespace}/stats/{statCode}

from accelbyte_py_sdk.api.social import update_stat
from accelbyte_py_sdk.api.social.models import StatUpdate

result, error = update_stat(
    stat_code=stat_code,
    body=StatUpdate.create(
        name="<your-new-name>",
    ),
)
if error:
    print(error)