Rocket.Chat icon indicating copy to clipboard operation
Rocket.Chat copied to clipboard

How to create user using REST APi?

Open sudhirkumargpt opened this issue 10 years ago • 2 comments

How to create user using REST APi?

sudhirkumargpt avatar Apr 05 '16 10:04 sudhirkumargpt

This is something I wrote quickly to bulk add users from my company database to rocket.chat

`<?php

add users to an array.

$user = array(array('email' => '[email protected]', 'pass' => 'somepassword', 'name' => 'some.username'));

$users =array('users' => $user);

login function

function talklogin($fuser, $pass) {

$service_url = 'http://your.rocketchat.url/api/login'; $curl = curl_init($service_url); $curl_post_data = array( "password" => $pass, "user" => $fuser, ); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($curl_post_data)); $curl_response = curl_exec($curl); curl_close($curl); return json_decode($curl_response); }

create user function, it takes array of users and login token as parameters.

function createuser($jusers, $me) { $service_url = 'http://your.rocketchat.url/api/bulk/register'; $curl = curl_init($service_url); $curl_post_data = json_encode($jusers); echo $curl_post_data; curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'X-Auth-Token: '.$me->data->authToken, 'X-User-Id: '.$me->data->userId, "Content-Type: application/json" )); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data); $curl_response = curl_exec($curl); curl_close($curl); return json_decode($curl_response); }

do the login

$me = talklogin('youradminuser', 'youradminpassword');

if login is OK, create users ...

if ($me->status == 'success') { echo 'loggedin:'; $de = createuser($users, $me); print_r($de); } else { echo 'error: '. $de->status; }

`

sinisadukaric avatar May 04 '16 13:05 sinisadukaric

Please see here: https://rocket.chat/docs/developer-guides/rest-api/ for details on using the rest api.

geekgonecrazy avatar Oct 27 '16 20:10 geekgonecrazy