Create attribute
You can create custom attributes for any model.
Possible attribute types: boolean, date, datetime, fixed, float, integer, json, string.
Attributes
Optional unless marked required
- content_type
- Records the attribute is for:
activity,clientuser(contacts),company,deal,productorticket. Contacts areclientuserhere, butuserin Get all attributes. - name
- Name of the attribute. Can't be empty, and must be unique among the model's attributes, ignoring letter case. The API doesn't enforce a maximum length.
- value_type
- Type of the attribute's values:
boolean,date,datetime,fixed,float,integer,jsonorstring.stringwhen not sent. - choices
- Options of a
fixedattribute. Without them the attribute has no options. - allow_multiple_choices
- Whether more than one option of a
fixedattribute can be selected.falsewhen not sent.
Request
- cURL
- JavaScript
- PHP
- Python
curl -X POST "https://<your_app_subdomain>.user.com/api/public/attributes/" \
-H "Authorization: Token <your_64_char_api_key>" \
-H "Accept: */*; version=2" \
-H "Content-Type: application/json" \
-d '{
"name": "Segment",
"value_type": "string",
"content_type": "company"
}'
const response = await fetch(
"https://<your_app_subdomain>.user.com/api/public/attributes/",
{
method: "POST",
headers: {
Authorization: "Token <your_64_char_api_key>",
Accept: "*/*; version=2",
"Content-Type": "application/json",
},
body: JSON.stringify({
name: "Segment",
value_type: "string",
content_type: "company",
}),
},
);
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/attributes/",
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([
"name" => "Segment",
"value_type" => "string",
"content_type" => "company",
]),
]);
$response = curl_exec($curl);
curl_close($curl);
echo $response;
import requests
response = requests.post(
"https://<your_app_subdomain>.user.com/api/public/attributes/",
headers={
"Authorization": "Token <your_64_char_api_key>",
"Accept": "*/*; version=2",
},
json={
"name": "Segment",
"value_type": "string",
"content_type": "company",
},
)
print(response.json())
Response examples
- 201 Created
- 400 Bad Request
- 409 Conflict
{
"id": 46,
"name": "API string attribute",
"name_std": "api_string_attribute",
"value_type": "string",
"content_type": "clientuser"
}
{"value_type": ["'not-a-type' is not a valid value type choice. Possible choices are: boolean, fixed, string, date, datetime, integer, float, json"]}{
"errors": "Field with such name already exists"
}
A 400 names every field that was rejected, each with its messages:
| Message | When |
|---|---|
This field is required. | name or content_type is missing. |
This field may not be blank. | name is empty. |
'abc' is not a valid content type choice. Possible choices are: clientuser, activity, company, deal, product, ticket | content_type isn't one of those, user included. |
'abc' is not a valid value type choice. Possible choices are: boolean, fixed, string, date, datetime, integer, float, json | value_type isn't one of those. |
Expected a list of items but got type "str". | choices isn't a list. |
The 409 comes back when the model already has an attribute with that name, also when only the letter case differs. The same name on another model is fine. choices sent for a type other than fixed are saved but have no effect.
Attributes can't be deleted through the API: remove one in the panel.
Response fields
Fields returned in the 201 Created response body.
- id
- Unique identifier of the attribute, assigned by User.
- name
- Name of the attribute, as sent.
- name_std
- Standardized name used in the API and in record bodies (
Loyalty tierbecomesloyalty_tier). - value_type
- Type of the attribute's values:
boolean,date,datetime,fixed,float,integer,jsonorstring. - content_type
- Records the attribute is for, as sent:
activity,clientuser,company,deal,productorticket.
Errors that can come from any endpoint (401, 429, 500, 502, 504) are described in HTTP response codes.
Fixed Choice Attributes Example
Request
- cURL
- JavaScript
- PHP
- Python
curl -X POST "https://<your_app_subdomain>.user.com/api/public/attributes/" \
-H "Authorization: Token <your_64_char_api_key>" \
-H "Accept: */*; version=2" \
-H "Content-Type: application/json" \
-d '{
"content_type": "clientuser",
"name": "Newsletter preferences",
"value_type": "fixed",
"choices": ["Daily digest", "Weekly summary", "Monthly newsletter", "Product updates"],
"allow_multiple_choices": true
}'
var data = JSON.stringify({
"content_type": "clientuser",
"name": "Newsletter preferences",
"value_type": "fixed",
"choices": ["Daily digest", "Weekly summary", "Monthly newsletter", "Product updates"],
"allow_multiple_choices": true
});
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/attributes/");
xhr.setRequestHeader("Authorization", "Token <your_64_char_api_key>");
xhr.setRequestHeader("Accept", "*/*; version=2");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(data);
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://<your_app_subdomain>.user.com/api/public/attributes/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode(array(
"content_type" => "clientuser",
"name" => "Newsletter preferences",
"value_type" => "fixed",
"choices" => array("Daily digest", "Weekly summary", "Monthly newsletter", "Product updates"),
"allow_multiple_choices" => true
)),
CURLOPT_HTTPHEADER => array(
"Accept: */*; version=2",
"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
import json
url = "https://<your_app_subdomain>.user.com/api/public/attributes/"
payload = json.dumps({
"content_type": "clientuser",
"name": "Newsletter preferences",
"value_type": "fixed",
"choices": ["Daily digest", "Weekly summary", "Monthly newsletter", "Product updates"],
"allow_multiple_choices": True
})
headers = {
'Authorization': 'Token <your_64_char_api_key>',
'Accept': '*/*; version=2',
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
Response
{
"id": 47,
"name": "Newsletter preferences",
"name_std": "newsletter_preferences",
"value_type": "fixed",
"content_type": "clientuser"
}
The response doesn't include choices or allow_multiple_choices. Get all attributes lists them for the model (/all-attributes/user/ for contacts).