REST API - avatar creation from selfie

Is it possible to use a REST API to generate an avatar from a selfie? For instance, can I upload a selfie to create an avatar template?

I’ve been searching for documentation or any other resources on this topic, but I haven’t been able to find any.

I would greatly appreciate your assistance with this matter. Thank you!

  1. Create a guest user

2. Create avatar draft with selfie:

POST https://api.readyplayer.me/v2/avatarswith the following request body:

{
    "data": {
        "userId": {rpmUserId}
        "partner": "{theirSubdomain}",
        "bodyType": "fullbody"
        "base64Image: {base64EncodedSelfie},
        "gender": {optionalGender},
        "assets": {
            "outfit": "{assetID}"
        }
    }
}

By calling this endpoint with similar data as shown in the example in the request body, you can create an avatar draft for the RPM user.

gender property is optional - if it’s provided in the request body, it will be applied. Otherwise it will be determined by the provided photo.

Assets can also be applied here if you wish to add a specific outfit to the avatar.

The metadata of the freshly created avatar draft will be returned in the response. Then you should take the avatarId value from the response to save the avatar draft.

3. Save avatar draft: Saves the avatar draft to an actual avatar.

PUT https://api.readyplayer.me/v2/avatars/{avatarId}

4. To fetch the 3D model:

Finally, you can either use the avatarId value to retrieve the actual 3D model of the saved avatar… or alternatively, you could just open our avatar editor with the user and you should see the avatar there.

GET https://models.readyplayer.me/{avatarId}.glb

Will this also work for anonymous accounts?
What is the POST endpoint in if I am following this guide?

Hi Sean,

POST is a method of HTTP, and to create an avatar, the API endpoint is https://api.readyplayer.me/v2/avatars.

Sure - but it doesn’t seem to work with an anonymous avatar id + token?
Any example, tutorial or code snippet for this with this flow?

I can create a new avatar with selfie, but another problem appears: ASSETS in POST cannot be saved. Here is a testing python code:

import requests
import base64

CREATE_ANONYMOUS_USER = True
TEMPLATE_SELFIE = True
FETCH_AVATAR_PREVIEW = False
SAVE_AVATAR = True
FETCH_AVATAR = True

DOMAIN = 'demo'


def save_glb_from_bytes(glb_bytes, glb_path = 'test.glb'):
    with open(glb_path, "wb") as binary_file:
        # Write bytes to file
        binary_file.write(glb_bytes)


if CREATE_ANONYMOUS_USER:
    r = requests.post('https://' + DOMAIN + '.readyplayer.me/api/users')
    print('CREATE_ANONYMOUS_USER', r)

    user_json = r.json()
    auth_token = user_json['data']['token']
    user_id = user_json['data']['id']


if TEMPLATE_SELFIE:
    with open("image.png", "rb") as image_file:
        image_str = base64.b64encode(image_file.read()).decode()

    headers = {'Authorization': 'Bearer ' + auth_token}
    body = {
        "data": {
            "userId": user_id,
            "partner": DOMAIN,
            "bodyType": "fullbody",
            "base64Image": image_str,
            "assets": {
                "outfit": "120333144",
            }
        }
    }

    r = requests.post('https://api.readyplayer.me/v2/avatars', headers=headers, json=body)
    assign_temp_json = r.json()
    print('TEMPLATE_SELFIE', r)

    avatar_id = assign_temp_json['data']['id']


if FETCH_AVATAR_PREVIEW:
    r = requests.get('https://api.readyplayer.me/v2/avatars/' + avatar_id + '.glb?preview=true')
    print('FETCH_AVATAR_PREVIEW', r)

    save_glb_from_bytes(r.content)


if SAVE_AVATAR:
    headers = {'Authorization': 'Bearer ' + auth_token}
    r = requests.put('https://api.readyplayer.me/v2/avatars/' + avatar_id, headers=headers)
    save_json = r.json()
    print('SAVE_AVATAR', r)


if FETCH_AVATAR:
    r = requests.get('https://models.readyplayer.me/' + avatar_id + '.glb')
    print('FETCH_AVATAR', r)

    save_glb_from_bytes(r.content)

In assign_temp_json , the ID of outfit is set correctly, but in save_json, the ID of outfit falls back to the default value

Can any one help to figure out where the problem is?

You’re using the demo subdomain. You need permissions on the subdomain. You can create one in Studio.readyplayer.me if you haven’t yet.

the GET avatar endpoints are publicly available and currently do not require authentication.
All other Endpoints require an API Key. See Authentication.

Thank you for your reply, I have registered a subdomain, I changed it to “demo” subdomain before I sent out the code.

I has test both of demo domain and my registered domain, and the API return the key successfully and produce exactly the same result.

I think the problem is in V2 EQUIP API. I follow the guide (Custom Avatar Creator | Ready Player Me), and only the EQUIP step does not provide the expected result.

Same problem in this post (Trouble equipping assets - #4 by wantr0ba).

Hi,
I would like to create an avatar from a selfie.
I could generate an avatar draft from an image and get the avatarID, but using an string.empty for assetID in the request body, as well as the token from the guest user response.
I currently cannot permanently save the avatar and get a 404 error. To save the avatar draft:

string url = string url = $"https://api.readyplayer.me/v2/avatars/{avatarId}";
using UnityWebRequest request = new UnityWebRequest(url, "PUT");
request.SetRequestHeader("Authorization", $"Bearer {guestUserToken}");

Is using assetID for the outfit mandatory?
I would appreciate any help. Thanks in advance

Hi @Zenway and @sean_hxq , I am running into the same issue. For a guest user, I cannot save with PUT a draft avatarId that was generated with a selfie.
Have you been able to find a solution? I would appreciate any hint.