This solution article will help you figure out how to create and update contacts that have custom fields with APIs.
STEP 1: Get the name of the custom field.
To get the name of the custom field Department, the following API call would help you. This API will list all the contact fields both default and custom.
Request URI: http://domain.freshdesk.com/api/v2/contact_fields
Request Method: GET
Sample Code:
curl -u API_KEY:X -X GET http://domain.freshdesk.com/api/v2/contact_fields
Sample Response:
[ { "editable_in_signup": false, "id": 14, "label": "Department", "name": "department", "position": 1, "type": "custom_text", "default": false, "customers_can_edit": true, "label_for_customers": "Department", "required_for_customers": false, "displayed_for_customers": true, "required_for_agents": false, "created_at": "2016-02-05T14:02:59Z", "updated_at": "2016-02-05T14:02:59Z" }, { "editable_in_signup": true, "id": 1, "label": "Full Name", "name": "name", "position": 2, "type": "default_name", "default": true, "customers_can_edit": true, "label_for_customers": "Full Name", "required_for_customers": true, "displayed_for_customers": true, "required_for_agents": true, "created_at": "2016-01-04T09:19:44Z", "updated_at": "2016-01-04T09:19:44Z" }, { "editable_in_signup": false, "id": 2, "label": "Title", "name": "job_title", "position": 3, "type": "default_job_title", "default": true, "customers_can_edit": true, "label_for_customers": "Title", "required_for_customers": false, "displayed_for_customers": true, "required_for_agents": false, "created_at": "2016-01-04T09:19:44Z", "updated_at": "2016-01-04T09:19:44Z" }, ]
You can filter the contact field which has label tag as Department. In the above example, the name of our custom field is department.
Step 2: Create contact with custom fields
Request URI: http://domain.freshdesk.com/api/v2/contacts
Request Method: POST
Sample Code:
curl -u API_KEY:X -H "Content-Type: application/json" -X POST -d ‘{ “name” : “Clark Kent”, “email” : “superman@freshdesk.com”, “custom_fields” : { “department” : “Superhero” } }’ 'https://domain.freshdesk.com/api/v2/contacts’
Sample Response:
{ "active":false, "address":null, "company_id":null, "deleted":false, "description":null, "email":"superman@freshdesk.com", "id":120, "job_title":null, "language":"en", "mobile":null, "name":"Clark Kent", "phone":null, "time_zone":"Chennai", "twitter_id":null, "custom_fields":{ "department":"Superhero" }, "tags":[ ], "other_emails":[ ], "created_at":"2016-02-05T14:09:05Z", "updated_at":"2016-02-05T14:09:05Z", "avatar":null }
Step 3: Update Contact with Custom Fields
Request URI: http://domain.freshdesk.com/api/v2/contacts/[id]
Request Method: PUT
Sample Code:
curl -u API_KEY:X -H "Content-Type: application/json" -X PUT -d ‘{ “description” : “Works in Daily Planet”, "job_title":”Journalist” , “custom_fields” : { “department” : “Justice League” } }’ 'http://domain.freshdesk.com/api/v2/contacts/8
Sample Response:
{ "active":false, "address":null, "company_id":null, "deleted":false, "description":"Works in Daily Planet", "email":"superman@freshdesk.com", "id":120, "job_title":"Journalist", "language":"en", "mobile":null, "name":"Clark Kent", "phone":null, "time_zone":"Chennai", "twitter_id":null, "custom_fields":{ "department":"Justice League" }, "tags":[ ], "other_emails":[ ], "created_at":"2016-02-05T14:09:05Z", "updated_at":"2016-02-05T14:09:05Z", "avatar":null }