All Collections
Working with Cloud Code
How do impersonate a user using the brainCloud API?
How do impersonate a user using the brainCloud API?

Invoking API calls as a specific user in cloud code

Paul Winterhalder avatar
Written by Paul Winterhalder
Updated over a week ago

This is a common question - especially for multiplayer experiences where scripts may need to update the state / balances of multiple user accounts.

Note - This technique is also useful for scheduled (and S2S) scripts that need to do some work on behalf of a specific user.

There are three steps to invoking client API methods as a specific user:

  1. Create a client session using the user's profileId 

  2. Obtain a service proxy for that session

  3. Make the API call

So - for example, if I wanted to award currency to a specific player who's profileId is winnersProfileId , here's what the code would look like:

// First get a client session
var winnersSession = bridge.getSessionForProfile( winnersProfileId );

// Get the currency service proxy - note that we pass in the session id!
var winnersCurrencyProxy = bridge.getVirtualCurrencyServiceProxy( winnersSession);

// Now award the winnings!
var result = winnersCurrencyProxy.awardCurrency( "coins", 200 );

Ta-dah!

Happy Coding!

Did this answer your question?