Users

Last Edit: Nov 13 2020

Stream allows you to store user information and embed them inside activities or use them for personalization. When stored in activities, users are automatically enriched by Stream.


const client = stream.connect('YOUR_API_KEY', 'FEED_TOKEN', 'APP_ID');

// ensure the user data is stored on Stream
await client.setUser({
    name: 'John Doe', 
    occupation: 'Software Engineer',
    gender: 'male'
});
                    

# The Python API client exposes users methods on client.users
# when adding reactions, collections or reading feeds with enrichment
# you can provide `user_id` as an argument
                    

# The Ruby API client exposes user method on client.users
# when adding reactions, collections or reading feeds with enrichment
# you can provide `:user_id` as an argument
                    

// The PHP API client exposes users methods on $client->users()
// when adding reactions, collections or reading feeds with enrichment
// you can provide `$user_id` as argument
                    

// Java API client exposes users methods on client.user object
// when adding reactions, collections you can provide `userID` as argument
                    

let client = Client(apiKey: "c38bmrz86x8y", appId: "64184", token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoidXNlcjEifQ.BQudnXB4B8wyoVDW403IaRG3zDs31L65VrVtf87prho")
let user = User(id: "john-doe")

client.create(user: user) { result in
    if let createdUser = try? result.get() {
        client.currentUser = createdUser
    }
}
                    

Adding Users

This endpoint allows you to insert a new user.

Name Type Description
id string The unique identifier for the new user (eg. username, user id, etc.). The value is restricted to alphanumeric characters, dashes and underscore symbols.
data object The data related to the user.

// create a new user, if the user already exist an error is returned
client.user("john-doe").create({
    name: "John Doe", 
    occupation: "Software Engineer",
    gender: 'male'
});

// get or create a new user, if the user already exist the user is returned
client.user("john-doe").getOrCreate({
    name: "John Doe", 
    occupation: "Software Engineer",
    gender: 'male'
});
                    

# create a new user, if the user already exist an error is returned
client.users.add(
    "john-doe",
    {"name": "John Doe", "occupation": "Software Engineer", "gender": "male"},
)

# get or create a new user, if the user already exist the user is returned
client.users.add(
    "john-doe",
    {"name": "John Doe", "occupation": "Software Engineer", "gender": "male"},
    get_or_create=True,
)
                    

# create a new user, if the user already exists, an error is returned
client.users.add(
    "john-doe", 
    :data => {:name => "John Doe", :occupation => "Software Engineer", :gender => "male"},
)

# get or create a new user, if the user already exists, the user is returned
client.users.add(
    "john-doe", 
    :data => {:name => "John Doe", :occupation => "Software Engineer", :gender => "male"},
    :get_or_create => true,
)
                    

// create a new user with the name in userdata
$user = $client->users()->add('42', array('name' => 'Arthur Dent'));
// get OR create the user
$user = $client->users()->add('42', array('name' => 'Arthur Dent'), true);
                    

// create a new user, if the user already exist an error is returned
client.user("john-doe").create(new Data()
        .set("name", "John Doe")
        .set("occupation", "Software Engineer")
        .set("gender", "male")).join();

// get or create a new user, if the user already exist the user is returned
client.user("john-doe").getOrCreate(new Data()
        .set("name", "John Doe")
        .set("occupation", "Software Engineer")
        .set("gender", "male")).join();
                    

// create a new user, if the user already exist an error is returned
userData := stream.User{
	ID: "john-doe",
	Data: map[string]interface{}{
		"name":       "John Doe",
		"occupation": "Software Engineer",
		"gender":     "male",
	},
}
user, err := client.Users().Add(userData, false)

// get or create a new user, if the user already exist the user is returned
user, err = client.Users().Add(userData, true)
                    

// create a new user, if the user already exist an error is returned
client.create(user: User(id: "john-doe"), getOrCreate: false) { result in /* ... */ }

// get or create a new user, if the user already exist the user is returned
client.create(user: User(id: "john-doe"), getOrCreate: true) { result in /* ... */ }
                    

// create a new user, if the user already exist an error is returned
const userData = new Dictionary<string, object>()
{
    {"name", "John Doe" },
    {"occupation", "Software Engineer"},
    {"gender", "male"},
};

await client.Users.Add("john-doe", userData);

// get or create a new user, if the user already exist the user is returned
await client.Users.Add("john-doe", userData, true);
                    

Retrieving Users

The retrieving users endpoint allows you to retrieve a user by their ID.


client.user('123').get();
                    

client.users.get("123")
                    

client.users.get("123")
                    

$client->users()->get('42');
                    

client.user("123").get().join();
                    

user, err := client.Users().Get("123")
                    

client.get(userId: "123") { result in /* ... */ }
                    

await client.Users.Get("123");
                    

Removing Users

The delete endpoint removes a user by their ID.


client.user('123').delete();
                    

client.users.delete("123")
                    

client.users.delete("123")
                    

$client-&gt;users()-&gt;delete('42');
                    

client.user("123").delete().join();
                    

err := client.Users().Delete("123")
                    

client.delete(userId: "123") { result in /* ... */ }
                    

await client.Users.Delete("123")
                    
When you delete a user be converted to a missing reference error when enriching.

Updating Users

This endpoint allows you to update a user by their ID.

Parameters

Name Type Description Default Optional
id string The ID of the user to update -
data object The data related to the user -

client.user('123').update({ name: "Jane Doe", occupation: "Software Engineer", gender: "female" });
                    

client.users.update("123",
    {"name": "Jane Doe", "occupation": "Software Engineer", "gender": "female"}
)
                    

client.users.update("123", 
    :data => {:name => "Jane Doe", :occupation => "Software Engineer", :gender => "female"}
)
                    

$user =  $client-&gt;users()-&gt;update('42', array('name' =&gt; 'Arthur Dent');
                    

client.user("123").update(new Data()
        .set("name", "Jane Doe")
        .set("occupation", "Software Engineer")
        .set("gender", "female")).join();
                    

data := map[string]interface{}{
	"name":       "John Doe",
	"occupation": "Software Engineer",
	"gender":     "female",
}
user, err := client.Users().Update("123", data)
                    

client.update(user: User(id: "john-doe")) { result in /* ... */ }
                    

var userData = new Dictionary<string, object="">()
{
    {"name", "Jane Doe" },
    {"occupation", "Software Engineer"},
    {"gender", "female"},
};
await client.Users.Update("123", userData);</string,>