How to Create Leads in Vtiger CRM Using API
This is something i wish i had when i started working with Vtiger, the official documentation is quite frustrating.
The instruction was written for open source Vtiger CRM version 6.5.0.
Preparation
- URL for all requests is
[crm_url]/webservice.php
- every user in Vtiger has User Name (in 'User Login & Role') and Access Key (in 'User Advanced Options') - we will need them both for authorisation. The User's Role needs to have enough rights to create leads.
include/Webservices/Create.php
- this file receives API calls for creating leads, use it to write additional functionality and verifications.
Authorisation
1 challenge to get token
[crm_url]/webservice.php?operation=getchallenge&username=[username]
request:
{
"operation": "getchallenge",
"username": "[username]"
}
response:
{
"success": true,
"result": {
"token": "5e71ff1288a6c",
"serverTime": 1584529170,
"expireTime": 1584529470
}
}
2 login to get sessionName
POST, parameters in body.
[crm_url]/webservice.php?operation=login&username=[username]&accessKey=[md5(challenge_token + accesskey)]
request:
{
"operation": "login",
"username": "[username]",
"accessKey": md5(challenge_token + [accesskey]
}
response:
{
"success": true,
"result": {
"SessionName": "3fc564805d9eeedcb9181",
"userId": "19x97",
"version": "0.22",
"vtigerVersion": "6.5.0"
}
}
Create lead
POST, parameters in body. elementType is case sensitive, write ‘Leads'.
request:
{
"operation":"create",
"format":"json",
"sessionName":"221747465d9f2d70a76e9",
"elementType":"Leads",
"element": {
"firstname":"test",
"lastname":"test", //mandatory
"email":"email@example.com",
"phone":"123456789",
"country":"Ukraine",
"website":"https://test.com/",
//list of mandatory fields is unique to your crm
"assigned_user_id":"19x97" // this is parameter from authorisation response
}
}
in response you will get all lead's fields
Get lead
GET. You can use 'query' operation for this. it functions like a standart mysql request so you can experiment with different conditions.
request:
{
"operation": "query",
"sessionName": "[SessionName]",
"query": "SELECT id FROM Leads;"
}
more query examples:
SELECT * FROM Leads;
SELECT id FROM Leads WHERE id IN ('10x31211', '10x31212');
,
SELECT id FROM Leads WHERE country != ' ' LIMIT 100;