Update or Create user


Create or Update user based on "user_id" value. If user with provided value exists, we will update his profile with provided data, otherwise new user will be created.

Allowed attributes: assigned_to, city, company_id, country, email, facebook_url, first_name, gender, google_url, gravatar_url, last_name, linkedin_url, notifications, phone_number, region, score, status, timezone, twitter_url, unsubscribed, user_id.

You can also set up any custom attribute of your choice. If specified custom attribute doesn't exist yet it will be created.

If you want to also add tags to the user just pass them as a list of strings. If given tag doesn't exists it will be created. It only adds tags. If the user has a tag not specified it will not be removed.

POST

/users/update_or_create/

Data
Type
Description
user_id
string
Custom and unique identifier for user
email
string
Define an email address for user
first_name
string
First name for a user
last_name
string
Last name for a user
company_id
string
Add user as employee to company with given company_id
custom_attr
string
Value of your custom attribute
tags
list of strings
Tags added to the user
curl -X POST -H "Authorization: Token <your_64_char_api_key>" -H "Content-Type: application/json" -d '{
  "user_id": "my_custom_id",
  "first_name": "John",
  "last_name": "Doe",
  "tags": ["updated", "verified"]
}' "https://<your_app_subdomain>.user.com/api/public/users/update_or_create/"

Response

{
  "created": true,
  "id": 5577
}
var settings = {
  "async": true,
  "crossDomain": true,
  "url": "https://<your_app_subdomain>.user.com/api/public/users/update_or_create/",
  "method": "POST",
  "headers": {
    "authorization": "Token <your_64_char_api_key>",
    "content-type": "application/json"
  },
  "data": {
    "user_id": "my_custom_id",
    "first_name": "John",
    "last_name": "Doe"
  }
}

$.ajax(settings).done(function (response) {
  console.log(response);
});
var data = "user_id=my_custom_id&first_name=John&last_name=Doe";

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
  if (this.readyState === 4) {
    console.log(this.responseText);
  }
});

xhr.open("POST", "https://<your_app_subdomain>.user.com/api/public/users/update_or_create/");
xhr.setRequestHeader("authorization", "Token <your_64_char_api_key>");
xhr.setRequestHeader("content-type", "application/json");

xhr.send(data);
var request = require("request");

var options = { method: 'POST',
  url: 'https://<your_app_subdomain>.user.com/api/public/users/update_or_create/',
  headers: {
    'content-type': 'application/json',
    authorization: 'Token <your_64_char_api_key>'
  },
  form: {
    user_id: 'my_custom_id',
    first_name: 'John',
    last_name: 'Doe'
  }
};

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});
<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://<your_app_subdomain>.user.com/api/public/users/update_or_create/",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\n  \"user_id\": \"my_custom_id\",\n  \"first_name\": \"John\",\n  \"last_name\": \"Doe\"\n}",
  CURLOPT_HTTPHEADER => array(
    "authorization: Token <your_64_char_api_key>",
    "content-type: application/json"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
import requests

url = "https://<your_app_subdomain>.user.com/api/public/users/update_or_create/"

payload = "{\n\"user_id\": \"my_custom_id\", \n\"first_name\":\"John\", \n\"last_name\":\"Doe\" \n}"
headers = {
    'authorization': "Token <your_64_char_api_key>",
    'content-type': "application/json"
    }

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)