Admin API

The Admin API is an alternative method to administrate an MKNet environment, it can be particularly useful for automating tasks that are repetitive.

An example use case for the Admin API is uploading asset bundles whenever a game release has been built and tested. A script can be created to generate the asset bundles for the game and uploaded to MKNet using the admin APIs.

Set up #

All requests to the Admin APIs must be authenticated. In order to authenticate requests, you will need an Active Directory application created for your project. You will need to ask the backend team to create one of these for you.

You will be given the following details:

  • Client Id
  • Client Secret
  • Tenant Id
  • Scope for your projects Admin API. There will likely be two, one for staging and one for production.
  • URL of your projects Admin API. There will likely be two, one for staging and one for productionIt is very important that the client secret is kept secret, do not share it around unnecessarily. If giving these details to Mighty Games, ask them to store the secret as a secret in their build system.
Making API Request to the Admin API #

Example of making a request to an Admin API:

CLIENT_ID="[YOUR CLIENT ID]"
CLIENT_SECRET="[YOUR CLIENT SECRET]" #Pull this out of an environment variable, or secure secret storage
SCOPE="[YOUR ADMIN API SCOPE]"
TENANT_ID="[YOUR TENANT ID]"
ADMIN_API_URL="[YOUR ADMIN API URL]"
HOST_HEADER="Host: ${ADMIN_API_URL/https:\/\/}"
AUTH_REQUEST_BODY="grant_type=client_credentials"
AUTH_REQUEST_BODY="${AUTH_REQUEST_BODY}&client_id=${CLIENT_ID}"
AUTH_REQUEST_BODY="${AUTH_REQUEST_BODY}&client_secret=${CLIENT_SECRET}"
AUTH_REQUEST_BODY="${AUTH_REQUEST_BODY}&scope=${SCOPE}"
AUTH_REQUEST_URL="https://login.microsoftonline.com/${TENANT_ID}/oauth2/v2.0/token"
TOKEN=$(curl "${AUTH_REQUEST_URL}" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "${AUTH_REQUEST_BODY}" \
| jq -r '.access_token')
curl -H "Authorization: Bearer ${TOKEN}" -H ${HOST_HEADER} \
"${ADMIN_API_URL}/api/SoloEvent/Search?pageNumber=1&pageSize=50" -i

Example of Uploading an Asset Bundle and waiting for completion

CLIENT_ID="[YOUR CLIENT ID]"
CLIENT_SECRET="[YOUR CLIENT SECRET]" #Pull this out of an environment variable, or secure secret storage
SCOPE="[YOUR ADMIN API SCOPE]"
TENANT_ID="[YOUR TENANT ID]"
ADMIN_API_URL="[YOUR ADMIN API URL]"
PATH_TO_ZIP="[YOUR ASSET BUNDLE ZIP FILE]"
HOST_HEADER="Host: ${ADMIN_API_URL/https:\/\/}"
AUTH_REQUEST_BODY="grant_type=client_credentials"
AUTH_REQUEST_BODY="${AUTH_REQUEST_BODY}&client_id=${CLIENT_ID}"
AUTH_REQUEST_BODY="${AUTH_REQUEST_BODY}&client_secret=${CLIENT_SECRET}"
AUTH_REQUEST_BODY="${AUTH_REQUEST_BODY}&scope=${SCOPE}"
AUTH_REQUEST_URL="https://login.microsoftonline.com/${TENANT_ID}/oauth2/v2.0/token"
# Get Auth Token
TOKEN=$(curl --silent "${AUTH_REQUEST_URL}" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "${AUTH_REQUEST_BODY}" \
| jq -r '.access_token')
# Upload the Asset Bundle
PROCESS_ID=$(curl --silent -H "Authorization: Bearer ${TOKEN}" \
-H "${HOST_HEADER}" \
-X POST \
"${ADMIN_API_URL}/api/AssetBundle/FileUpload" \
-F "file=@${PATH_TO_ZIP}" | jq -r .processId)
# Wait until asset has finished processing
FINISHED=$(curl --silent -H "Authorization: Bearer ${TOKEN}" -H "${HOST_HEADER}" "${ADMIN_API_URL}/api/AssetBundle/FileUploadStatus/${PROCESS_ID}" | jq -r .finished)
while [ "$FINISHED" != "true" ]; do
sleep 5
FINISHED=$(curl --silent -H "Authorization: Bearer ${TOKEN}" -H "${HOST_HEADER}" "${ADMIN_API_URL}/api/AssetBundle/FileUploadStatus/${PROCESS_ID}" | jq -r .finished)
done
# All finished, upload is complete!
# Note that although the upload is now complete, we didn't check if it completely successfully. To do this we could need to also check the finishedOk property on the response is also true