POST/ users/ update_or_create/
Update or create user
You can create or update users based on the "custom_id" key value. If a user with the provided value exists, we will update his profile with provided data, otherwise, a new user will be created. You can also set up any custom attribute of your choice. Custom attributes have to be created in the User app before sending them within requests.
Attributes
Optional unless marked required
- custom_id
- The unique identifier for the user which is provided by the client.
- company_custom_id
- A unique identifier for the company which is provided by the client.
- custom_attr
- An example value of your custom attribute.
- A user email address.
- first_name
- A first name for a user (the maximum number of characters is 40).
- last_name
- A last name for a user (the maximum number of characters is 40).
- tags
- A list of tag names.
Request
- cURL
- JavaScript
- PHP
- Python
curl -X POST "https://<your_app_subdomain>.user.com/api/public/users/update_or_create/" \
-H "Authorization: Token <your_64_char_api_key>" \
-H "Accept: */*; version=2" \
-H "Content-Type: application/json" \
-d '{
"custom_id": "alex",
"first_name": "John",
"last_name": "Doe"
}'
const response = await fetch(
"https://<your_app_subdomain>.user.com/api/public/users/update_or_create/",
{
method: "POST",
headers: {
Authorization: "Token <your_64_char_api_key>",
Accept: "*/*; version=2",
"Content-Type": "application/json",
},
body: JSON.stringify({
custom_id: "alex",
first_name: "John",
last_name: "Doe",
}),
},
);
const data = await response.json();
console.log(data);
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://<your_app_subdomain>.user.com/api/public/users/update_or_create/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => [
"Authorization: Token <your_64_char_api_key>",
"Accept: */*; version=2",
"Content-Type: application/json",
],
CURLOPT_POSTFIELDS => json_encode([
"custom_id" => "alex",
"first_name" => "John",
"last_name" => "Doe",
]),
]);
$response = curl_exec($curl);
curl_close($curl);
echo $response;
import requests
response = requests.post(
"https://<your_app_subdomain>.user.com/api/public/users/update_or_create/",
headers={
"Authorization": "Token <your_64_char_api_key>",
"Accept": "*/*; version=2",
},
json={
"custom_id": "alex",
"first_name": "John",
"last_name": "Doe",
},
)
print(response.json())
Response examples
- 200 OK
- 400 Bad Request
{
"created": true,
"id": 5577
}
{"email": ["Enter a valid email address."]}A 400 names every field that was rejected, each with its messages. The ones you're most likely to see:
| Message | When |
|---|---|
email: Enter a valid email address. | The value isn't an email address. |
gender: "abc" is not a valid choice. | The value isn't one of the gender values. |
<custom attribute>: A valid integer is required. | The value doesn't fit the attribute's type (a number attribute here). |
Without custom_id, every call creates a new contact ("created": true). A company_custom_id no company has is ignored.
Response fields
Fields returned in the 200 OK response body.
- created
trueif a new contact was created,falseif the contact with thiscustom_idwas updated.- id
- A unique identifier for the user which is given by User automatically.
note
Errors that can come from any endpoint (401, 429, 500, 502, 504) are described in HTTP response codes.