We’ve migrated our documentation to a new site, which means some URLs have changed.
Subscriptions

How to update User information via JavaScript

This guide explains how to update user information using the Piano API endpoint /id/api/v1/identity/userinfo via JavaScript. You can use this endpoint to modify user details such as name, email, password and custom fields.

A possible use case could be that the publisher doesn’t want to use our My Account features but still wants to give users the option to change their email or password. Instead of creating a back end to call the API, they can use this method on the front end. All they need to do is create a form with input fields where the user enters the data.

The examples below use the sandbox base URL. Other environments’ base URLs can be found here.

Updating First and Last Names

Although it is possible to use the endpoint for updating the user’s name, it is recommended to use the Identity Management function tp.pianoId.updateUser() as explained here.

Updating Emails

Please note, that to update the user's email via JavaScript, you would need to know the user's current password.

The user token is invalidated during the process, so it is necessary to use the tp.pianoId.loginByToken() method to keep the user logged in.

var newEmail = "new.email@piano.io"; 
var currentPassword = "password12345"; 

fetch('https://sandbox.piano.io/id/api/v1/identity/userinfo?lang=en_US' +  
  '&aid=' + tp.aid +  
  '&access_token=' + tp.pianoId.getToken(),  
  { 
    method: 'PUT', 
    headers: { 'Content-Type': 'application/json; charset=UTF-8' }, 
    body: JSON.stringify({ 
      uid: tp.pianoId.getUser().uid, 
      email: newEmail, 
      current_password: currentPassword 
    }) 
  } 
) 
.then(r => r.json()) 
.then(response => { 
  tp.pianoId.loginByToken(response.token); 
});

Updating Passwords

Please note, that to update the user's password via JavaScript, you would need to know the user's current password.

var newEmail = "newemail@piano.io";
var currentPassword = "Piano123456";
var newPassword = "123Piano456";

fetch('https://sandbox.piano.io/id/api/v1/identity/userinfo?lang=en_US' + 
  '&aid=' + tp.aid + 
  '&access_token=' + tp.pianoId.getToken(), 
  {
    method: 'PUT',
    headers: { 'Content-Type': 'application/json; charset=UTF-8' },
    body: JSON.stringify({
      uid: tp.pianoId.getUser().uid,
      email: newEmail,
      current_password: currentPassword,
      new_password: newPassword
    })
  }
);

Updating Custom Fields

This code updates custom fields for a user in the Piano database.

var customFields = [ 
  { 
    field_name: 'myCustomField', // Field ID, not a name of the CF  
    value: "ExampleValue" 
  },  
  { 
    field_name: 'another_cf', // Field ID, not a name of the CF  
    value: "ExampleValue" 
  } 
]; 

fetch('https://sandbox.piano.io/id/api/v1/identity/userinfo?lang=en_US' +  
  '&aid=' + tp.aid +  
  '&access_token=' + tp.pianoId.getToken(),  
  { 
    method: 'PUT', 
    headers: { 'Content-Type': 'application/json; charset=UTF-8' }, 
    body: JSON.stringify({ 
      uid: tp.pianoId.getUser().uid, 
      form_name: 'myCustomForm',
      custom_field_values: customFields 
    }) 
  } 
);

Here are the format examples of values for the different types of custom fields:

Field type

Formatting example

Text

Text

Date

'yyyy-mm-dd' or 'mm/dd/yyyy'

Number

1234

Checkbox

'true' or 'false'

Single-Select

["DE"]

Multi-Select

["Politics", "Sports"]

Scale

Not supported

Last updated: