> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.levrage.ai/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.levrage.ai/_mcp/server.

# Create or upsert a lead

POST https://api.levrage.ai/v1/crm/leads
Content-Type: application/json

Matches the person on `external_id`, then `phone`, then `email`; `on_conflict` decides what happens to a match. Custom fields with no field in the workspace are one 400 naming them all, or new text fields with `create_missing_fields`.

Reference: https://docs.levrage.ai/api-reference/crm/upsert-lead

## Request

### Headers

- `Idempotency-Key` (string, optional) — Your unique id for this request (≤ 255 characters). Repeating it within 24 h replays the first answer instead of writing twice.

### Body (application/json)

This endpoint expects an object.

- `name` (string, optional, nullable)
- `phone` (string, optional, nullable) — Indian numbers in any common shape: 9876543210, +91 98765 43210.
- `email` (string, optional, nullable)
- `external_id` (string, optional, nullable) — Your id for the person (customer no., loan no., employee id) — the best key to upsert on.
- `company` (string, optional, nullable)
- `city` (string, optional, nullable)
- `stage_id` (string, optional, nullable) — From GET /crm/stages.
- `pipeline_id` (string, optional, nullable) — From GET /crm/pipelines; without stage_id the lead goes to that pipeline's first stage.
- `owner_user_id` (string, optional, nullable)
- `tags` (list of string, optional, nullable)
- `score` (integer, optional, nullable)
- `score_label` (enum, optional, nullable)
  - Allowed values: `hot`, `warm`, `cold`
- `expected_close_at` (string, optional, nullable)
- `probability` (integer, optional, nullable)
- `lost_reason` (string, optional, nullable)
- `next_action_at` (string, optional, nullable)
- `consent` (CrmLeadsPostRequestBodyContentApplicationJsonSchemaConsent, optional, nullable)
- `fields` (CrmLeadsPostRequestBodyContentApplicationJsonSchemaFields, optional, nullable) — Custom fields by key or by label, in any case (GET /crm/fields lists them): \{"Zone": "Madhapur"}.
- `source` (string, optional, nullable) — On create only: a short lower-case word (default "api").
- `source_detail` (CrmLeadsPostRequestBodyContentApplicationJsonSchemaSourceDetail, optional, nullable) — On create only, e.g. \{"form": "Contact us"}.
- `on_conflict` (enum, optional, nullable) — When the person is already a lead: error (default, 409), update, or ignore.
  - Allowed values: `error`, `update`, `ignore`
- `match_on` (list of enum, optional, nullable) — What makes two leads the same person (default all three, in that order).
  - Allowed values: `external_id`, `phone`, `email`
- `create_missing_fields` (boolean, optional, nullable) — Custom field names that don't exist become text fields instead of a 400.

## Response

### 200

Matched an existing lead (updated or unchanged).

- `result` (enum, required)
  - Allowed values: `created`, `updated`, `unchanged`
- `lead` (CrmLead, required)
- `created_fields` (list of CrmCreatedField, optional, nullable)

### 201

Successful Response

- `result` (enum, required)
  - Allowed values: `created`, `updated`, `unchanged`
- `lead` (CrmLead, required)
- `created_fields` (list of CrmCreatedField, optional, nullable)

## Errors

### 400 Bad Request Error

Something in the request is wrong — `message` says what.

- `error` (CrmErrorBody, required)

### 401 Unauthorized Error

No key, or an unknown / revoked / expired key.

- `error` (CrmErrorBody, required)

### 403 Forbidden Error

The key has no CRM access, or read-only access.

- `error` (CrmErrorBody, required)

### 409 Conflict Error

Already a lead (on_conflict error) — `existing_id`.

- `error` (CrmErrorBody, required)

### 429 Too Many Requests Error

Over 600 requests a minute for this key — wait Retry-After seconds.

- `error` (CrmErrorBody, required)

## Types

### CrmLeadsPostRequestBodyContentApplicationJsonSchemaConsent

- `call` (boolean, optional, nullable)
- `whatsapp` (boolean, optional, nullable)
- `email` (boolean, optional, nullable)

### CrmLeadsPostRequestBodyContentApplicationJsonSchemaFields

Custom fields by key or by label, in any case (GET /crm/fields lists them): \{"Zone": "Madhapur"}.

### CrmLeadsPostRequestBodyContentApplicationJsonSchemaSourceDetail

On create only, e.g. \{"form": "Contact us"}.

### CrmLead

- `id` (string, required)
- `name` (string, optional, nullable)
- `phone` (string, optional, nullable)
- `email` (string, optional, nullable)
- `external_id` (string, optional, nullable)
- `company` (string, optional, nullable)
- `city` (string, optional, nullable)
- `source` (string, optional, nullable)
- `stage` (CrmStage, optional, nullable)
- `pipeline_id` (string, optional, nullable)
- `owner_user_id` (string, optional, nullable)
- `tags` (list of string, optional, default: [])
- `score` (integer, optional, nullable)
- `score_label` (string, optional, nullable)
- `fields` (CrmLeadFields, optional, default: {})
- `consent` (CrmLeadConsent, optional, default: {})
- `deal_value` (double, optional, nullable)
- `last_call_at` (string, optional, nullable)
- `last_call_outcome` (string, optional, nullable)
- `created_at` (string, optional, nullable)
- `updated_at` (string, optional, nullable)

### CrmCreatedField

- `key` (string, required)
- `label` (string, required)

### CrmErrorBody

- `code` (string, required)
- `message` (string, required)
- `existing_id` (string, optional, nullable)

### CrmStage

- `id` (string, required)
- `name` (string, required)
- `kind` (enum, required)
  - Allowed values: `open`, `qualified`, `won`, `lost`

### CrmLeadFields

### CrmLeadConsent

## Examples

### CRM_upsert_lead_example

**Request**

```json
{
  "name": "Amit Kumar",
  "phone": "9876543210",
  "external_id": "12345",
  "city": "Hyderabad",
  "fields": {
    "Zone": "Madhapur",
    "Week": "43",
    "Plan": "Gold"
  },
  "source": "google_sheet",
  "on_conflict": "update"
}
```

**Response**

```json
{
  "result": "created",
  "lead": {
    "id": "string",
    "name": "string",
    "phone": "string",
    "email": "string",
    "external_id": "string",
    "company": "string",
    "city": "string",
    "source": "string",
    "stage": {
      "id": "string",
      "name": "string",
      "kind": "open"
    },
    "pipeline_id": "string",
    "owner_user_id": "string",
    "tags": [
      "string"
    ],
    "score": 1,
    "score_label": "string",
    "fields": {},
    "consent": {},
    "deal_value": 1.1,
    "last_call_at": "string",
    "last_call_outcome": "string",
    "created_at": "string",
    "updated_at": "string"
  },
  "created_fields": [
    {
      "key": "string",
      "label": "string"
    }
  ]
}
```

**SDK Code**

```python CRM_upsert_lead_example
import requests

url = "https://api.levrage.ai/v1/crm/leads"

payload = {
    "name": "Amit Kumar",
    "phone": "9876543210",
    "external_id": "12345",
    "city": "Hyderabad",
    "fields": {
        "Zone": "Madhapur",
        "Week": "43",
        "Plan": "Gold"
    },
    "source": "google_sheet",
    "on_conflict": "update"
}
headers = {"Content-Type": "application/json"}

response = requests.post(url, json=payload, headers=headers)

print(response.json())
```

```javascript CRM_upsert_lead_example
const url = 'https://api.levrage.ai/v1/crm/leads';
const options = {
  method: 'POST',
  headers: {'Content-Type': 'application/json'},
  body: '{"name":"Amit Kumar","phone":"9876543210","external_id":"12345","city":"Hyderabad","fields":{"Zone":"Madhapur","Week":"43","Plan":"Gold"},"source":"google_sheet","on_conflict":"update"}'
};

try {
  const response = await fetch(url, options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}
```

```go CRM_upsert_lead_example
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io"
)

func main() {

	url := "https://api.levrage.ai/v1/crm/leads"

	payload := strings.NewReader("{\n  \"name\": \"Amit Kumar\",\n  \"phone\": \"9876543210\",\n  \"external_id\": \"12345\",\n  \"city\": \"Hyderabad\",\n  \"fields\": {\n    \"Zone\": \"Madhapur\",\n    \"Week\": \"43\",\n    \"Plan\": \"Gold\"\n  },\n  \"source\": \"google_sheet\",\n  \"on_conflict\": \"update\"\n}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("Content-Type", "application/json")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}
```

```ruby CRM_upsert_lead_example
require 'uri'
require 'net/http'

url = URI("https://api.levrage.ai/v1/crm/leads")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n  \"name\": \"Amit Kumar\",\n  \"phone\": \"9876543210\",\n  \"external_id\": \"12345\",\n  \"city\": \"Hyderabad\",\n  \"fields\": {\n    \"Zone\": \"Madhapur\",\n    \"Week\": \"43\",\n    \"Plan\": \"Gold\"\n  },\n  \"source\": \"google_sheet\",\n  \"on_conflict\": \"update\"\n}"

response = http.request(request)
puts response.read_body
```

```java CRM_upsert_lead_example
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;

HttpResponse<String> response = Unirest.post("https://api.levrage.ai/v1/crm/leads")
  .header("Content-Type", "application/json")
  .body("{\n  \"name\": \"Amit Kumar\",\n  \"phone\": \"9876543210\",\n  \"external_id\": \"12345\",\n  \"city\": \"Hyderabad\",\n  \"fields\": {\n    \"Zone\": \"Madhapur\",\n    \"Week\": \"43\",\n    \"Plan\": \"Gold\"\n  },\n  \"source\": \"google_sheet\",\n  \"on_conflict\": \"update\"\n}")
  .asString();
```

```php CRM_upsert_lead_example
<?php
require_once('vendor/autoload.php');

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://api.levrage.ai/v1/crm/leads', [
  'body' => '{
  "name": "Amit Kumar",
  "phone": "9876543210",
  "external_id": "12345",
  "city": "Hyderabad",
  "fields": {
    "Zone": "Madhapur",
    "Week": "43",
    "Plan": "Gold"
  },
  "source": "google_sheet",
  "on_conflict": "update"
}',
  'headers' => [
    'Content-Type' => 'application/json',
  ],
]);

echo $response->getBody();
```

```csharp CRM_upsert_lead_example
using RestSharp;

var client = new RestClient("https://api.levrage.ai/v1/crm/leads");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n  \"name\": \"Amit Kumar\",\n  \"phone\": \"9876543210\",\n  \"external_id\": \"12345\",\n  \"city\": \"Hyderabad\",\n  \"fields\": {\n    \"Zone\": \"Madhapur\",\n    \"Week\": \"43\",\n    \"Plan\": \"Gold\"\n  },\n  \"source\": \"google_sheet\",\n  \"on_conflict\": \"update\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

```swift CRM_upsert_lead_example
import Foundation

let headers = ["Content-Type": "application/json"]
let parameters = [
  "name": "Amit Kumar",
  "phone": "9876543210",
  "external_id": "12345",
  "city": "Hyderabad",
  "fields": [
    "Zone": "Madhapur",
    "Week": "43",
    "Plan": "Gold"
  ],
  "source": "google_sheet",
  "on_conflict": "update"
] as [String : Any]

let postData = JSONSerialization.data(withJSONObject: parameters, options: [])

let request = NSMutableURLRequest(url: NSURL(string: "https://api.levrage.ai/v1/crm/leads")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error as Any)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()
```

### CRM_upsert_lead_example

**Request**

```json
{
  "name": "Amit Kumar",
  "phone": "9876543210",
  "external_id": "12345",
  "city": "Hyderabad",
  "fields": {
    "Zone": "Madhapur",
    "Week": "43",
    "Plan": "Gold"
  },
  "source": "google_sheet",
  "on_conflict": "update"
}
```

**Response**

```json
{
  "result": "created",
  "lead": {
    "id": "string",
    "name": "string",
    "phone": "string",
    "email": "string",
    "external_id": "string",
    "company": "string",
    "city": "string",
    "source": "string",
    "stage": {
      "id": "string",
      "name": "string",
      "kind": "open"
    },
    "pipeline_id": "string",
    "owner_user_id": "string",
    "tags": [
      "string"
    ],
    "score": 1,
    "score_label": "string",
    "fields": {},
    "consent": {},
    "deal_value": 1.1,
    "last_call_at": "string",
    "last_call_outcome": "string",
    "created_at": "string",
    "updated_at": "string"
  },
  "created_fields": [
    {
      "key": "string",
      "label": "string"
    }
  ]
}
```

**SDK Code**

```python CRM_upsert_lead_example
import requests

url = "https://api.levrage.ai/v1/crm/leads"

payload = {
    "name": "Amit Kumar",
    "phone": "9876543210",
    "external_id": "12345",
    "city": "Hyderabad",
    "fields": {
        "Zone": "Madhapur",
        "Week": "43",
        "Plan": "Gold"
    },
    "source": "google_sheet",
    "on_conflict": "update"
}
headers = {"Content-Type": "application/json"}

response = requests.post(url, json=payload, headers=headers)

print(response.json())
```

```javascript CRM_upsert_lead_example
const url = 'https://api.levrage.ai/v1/crm/leads';
const options = {
  method: 'POST',
  headers: {'Content-Type': 'application/json'},
  body: '{"name":"Amit Kumar","phone":"9876543210","external_id":"12345","city":"Hyderabad","fields":{"Zone":"Madhapur","Week":"43","Plan":"Gold"},"source":"google_sheet","on_conflict":"update"}'
};

try {
  const response = await fetch(url, options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}
```

```go CRM_upsert_lead_example
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io"
)

func main() {

	url := "https://api.levrage.ai/v1/crm/leads"

	payload := strings.NewReader("{\n  \"name\": \"Amit Kumar\",\n  \"phone\": \"9876543210\",\n  \"external_id\": \"12345\",\n  \"city\": \"Hyderabad\",\n  \"fields\": {\n    \"Zone\": \"Madhapur\",\n    \"Week\": \"43\",\n    \"Plan\": \"Gold\"\n  },\n  \"source\": \"google_sheet\",\n  \"on_conflict\": \"update\"\n}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("Content-Type", "application/json")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}
```

```ruby CRM_upsert_lead_example
require 'uri'
require 'net/http'

url = URI("https://api.levrage.ai/v1/crm/leads")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n  \"name\": \"Amit Kumar\",\n  \"phone\": \"9876543210\",\n  \"external_id\": \"12345\",\n  \"city\": \"Hyderabad\",\n  \"fields\": {\n    \"Zone\": \"Madhapur\",\n    \"Week\": \"43\",\n    \"Plan\": \"Gold\"\n  },\n  \"source\": \"google_sheet\",\n  \"on_conflict\": \"update\"\n}"

response = http.request(request)
puts response.read_body
```

```java CRM_upsert_lead_example
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;

HttpResponse<String> response = Unirest.post("https://api.levrage.ai/v1/crm/leads")
  .header("Content-Type", "application/json")
  .body("{\n  \"name\": \"Amit Kumar\",\n  \"phone\": \"9876543210\",\n  \"external_id\": \"12345\",\n  \"city\": \"Hyderabad\",\n  \"fields\": {\n    \"Zone\": \"Madhapur\",\n    \"Week\": \"43\",\n    \"Plan\": \"Gold\"\n  },\n  \"source\": \"google_sheet\",\n  \"on_conflict\": \"update\"\n}")
  .asString();
```

```php CRM_upsert_lead_example
<?php
require_once('vendor/autoload.php');

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://api.levrage.ai/v1/crm/leads', [
  'body' => '{
  "name": "Amit Kumar",
  "phone": "9876543210",
  "external_id": "12345",
  "city": "Hyderabad",
  "fields": {
    "Zone": "Madhapur",
    "Week": "43",
    "Plan": "Gold"
  },
  "source": "google_sheet",
  "on_conflict": "update"
}',
  'headers' => [
    'Content-Type' => 'application/json',
  ],
]);

echo $response->getBody();
```

```csharp CRM_upsert_lead_example
using RestSharp;

var client = new RestClient("https://api.levrage.ai/v1/crm/leads");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n  \"name\": \"Amit Kumar\",\n  \"phone\": \"9876543210\",\n  \"external_id\": \"12345\",\n  \"city\": \"Hyderabad\",\n  \"fields\": {\n    \"Zone\": \"Madhapur\",\n    \"Week\": \"43\",\n    \"Plan\": \"Gold\"\n  },\n  \"source\": \"google_sheet\",\n  \"on_conflict\": \"update\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

```swift CRM_upsert_lead_example
import Foundation

let headers = ["Content-Type": "application/json"]
let parameters = [
  "name": "Amit Kumar",
  "phone": "9876543210",
  "external_id": "12345",
  "city": "Hyderabad",
  "fields": [
    "Zone": "Madhapur",
    "Week": "43",
    "Plan": "Gold"
  ],
  "source": "google_sheet",
  "on_conflict": "update"
] as [String : Any]

let postData = JSONSerialization.data(withJSONObject: parameters, options: [])

let request = NSMutableURLRequest(url: NSURL(string: "https://api.levrage.ai/v1/crm/leads")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error as Any)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()
```