Add the avatar editor to your site

Let your users create and change their own avatar without leaving your page. It is one request from your server and one line in your page.

You need a partner ID and key: register as a partner. Keep the key on your server and never put it in a web page or an app.

1Ask DoppelMe for an editor address

From your server, ask for an editor for one of your users. partner_user_id is your own id for the person (their user id or username). The first time you use an id, DoppelMe creates their avatar account for you, so there is nothing to set up first.

curl -X POST https://api.doppelme.com/v2/editor-sessions \
  -H "Authorization: Bearer YOUR_PARTNER_ID:YOUR_PARTNER_KEY" \
  -H "Content-Type: application/json" \
  -d '{"partner_user_id": "1001", "parent_origin": "https://www.example.com"}'
$ch = curl_init('https://api.doppelme.com/v2/editor-sessions');
curl_setopt_array($ch, [
    CURLOPT_POST           => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER     => [
        'Authorization: Bearer YOUR_PARTNER_ID:YOUR_PARTNER_KEY',
        'Content-Type: application/json',
    ],
    CURLOPT_POSTFIELDS     => json_encode([
        'partner_user_id' => (string) $user->id,         // your own id for this person
        'parent_origin'   => 'https://www.example.com',   // the address of your site
    ]),
]);
$doppelme    = json_decode(curl_exec($ch), true);
$editorUrl   = $doppelme['editor_url'];     // goes in your page (step 2)
$doppelmeKey = $doppelme['doppelme_key'];   // this person's avatar (step 4)
const response = await fetch('https://api.doppelme.com/v2/editor-sessions', {
  method: 'POST',
  headers: {
    Authorization: 'Bearer YOUR_PARTNER_ID:YOUR_PARTNER_KEY',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    partner_user_id: String(user.id),          // your own id for this person
    parent_origin: 'https://www.example.com',  // the address of your site
  }),
});
const doppelme = await response.json();
const editorUrl = doppelme.editor_url;       // goes in your page (step 2)
const doppelmeKey = doppelme.doppelme_key;   // this person's avatar (step 4)

The answer looks like this:

{
  "editor_url": "https://api.doppelme.com/editor#token=...",
  "doppelme_key": "DM102001ABC"
}

editor_url is good for two hours and goes in your page (step 2). doppelme_key is the person's avatar: keep it against your user so you can show their picture (step 4).

parent_origin is the address of the page that will show the editor, such as https://www.example.com. You can also add a return_url, the page the person goes back to after saving when the editor opens as a full page (on a phone, for example).

2Put it in your page

Add our script, and a div where you want the editor, with the address from step 1. That is the whole integration: the person dresses their avatar and presses Save.

<script src="https://api.doppelme.com/sdk/doppelme.js"></script>
<div data-doppelme-editor="EDITOR_URL"></div>

On a phone, where a frame is too small, it shows an "Edit your avatar" button that opens the editor as a full page.

3Know when they have saved (optional)

Listen for the editor's events when you want to react, for example to refresh the person's picture on your page.

document.addEventListener('doppelme:saved', function (event) {
  // event.detail.avatar_url is the picture of their new avatar
});

The other events are doppelme:ready, doppelme:change and doppelme:error.

4Show their avatar

Every avatar has a picture address made from its doppelme_key, the one from step 1:

<img src="https://www.doppelme.com/DOPPELME_KEY/avatar.png" alt="Avatar">

Use crop.png instead of avatar.png for head and shoulders.

Good to know

  • An editor address works for two hours. Ask for a fresh one each time you show the editor.
  • When the editor opens as a full page (on a phone, or with data-mode="page" on the div), the person comes back to the return_url from step 1 after saving.
  • More options go on the div: data-height="460" sets the height in pixels, data-auto-resize follows the editor's own height, and data-button-text changes the button's words.

Want to do more, such as dress avatars from your own code or make random ones? See the API reference, or ask us.