sdk.Leads creates leads in Rechat's CRM from your website, either directly with
capture(), or by binding an existing HTML form with form().
capture(channel, lead)const sdk = new Rechat.Sdk()
sdk.Leads.capture({
lead_channel: '2c55e69d-adbe-42d9-97a5-bedb43783b80'
}, {
first_name: 'John',
last_name: 'Doe',
email: 'john-doe@gmail.com',
phone_number: '+18189191212',
})
.then(lead => console.log('Created lead', lead.id))
.catch(e => console.error(e))
The first argument identifies where the lead goes; the second is the lead itself.
| Field | Description |
|---|---|
lead_channel |
The Rechat lead-channel id to deliver the lead to |
user_id, brand_id |
Attribute the lead to a user / brand |
ad_campaign_id, email_campaign_id, email_campaign_email_id |
Attribute the lead to a campaign |
Any of these fields present in the page URL as rechat_* query parameters
(e.g. ?rechat_ad_campaign_id=…) are collected automatically and merged in. Values
you pass explicitly win.
All optional unless noted: first_name, last_name, email, phone_number, tag,
lead_source, note, address, referer_url, mlsid, listing_id, agent_mlsid,
source_type, and assignees, a list of { email, phone_number, first_name, last_name, mlsid, mls } objects identifying agents the lead should be assigned to.
Resolves with the created lead: { id, contact, created_at, lead_channel, source_type }.
If the browser is offline it resolves with { queued: true, … } and the request is
re-sent automatically when the connection returns. See offline sync in the
SDK overview. Unless the tracker is disabled, the new lead is also
identified for activity tracking.
form(element, channel, events): bind an HTML formWire a form you already have to lead capture. On submit, every named field is collected
into the lead payload and passed to capture() with your channel:
<form id="lead-form">
<input name="first_name">
<input name="last_name">
<input name="email">
<button type="submit">Contact me</button>
</form>
<script>
const sdk = new Rechat.Sdk()
sdk.Leads.form(document.querySelector('#lead-form'), {
lead_channel: '2c55e69d-adbe-42d9-97a5-bedb43783b80'
}, {
validate: (payload) => Boolean(payload.email),
onSuccess: (lead) => console.log('Created', lead.id),
onError: (e) => console.error(e),
})
</script>
| Callback | When |
|---|---|
validate(payload) |
Before submission. Return false to cancel the capture. |
onSuccess(lead) |
The lead was created (or queued offline). |
onError(error) |
The capture failed. |
Phone: format and validate phone numberssdk.Leads.Phone parses whatever a visitor typed into a phone field, validates it, and
returns the number in a canonical form. Use it before calling capture() so the CRM
always receives a valid E.164 number.
const sdk = new Rechat.Sdk()
sdk.Leads.Phone.isValid('502-555-0123')
// true
sdk.Leads.Phone.format('502-555-0123')
// '+15025550123'
sdk.Leads.Phone.format('502-555-0123', 'national')
// '(502) 555-0123'
sdk.Leads.Phone.parse('+44 20 7946 0958')
// {
// e164: '+442079460958',
// national: '020 7946 0958',
// international: '+44 20 7946 0958',
// country: 'GB',
// calling_code: '44',
// extension: undefined
// }
sdk.Leads.Phone.parse('555-0123')
// null
The same helper is available without an Sdk instance as Rechat.LeadPhone on the
CDN build, or LeadPhone from the ESM package:
const phone = new Rechat.LeadPhone({ default_country: 'CA' })
phone.format('(416) 555-0199')
// '+14165550199'
| Method | Returns |
|---|---|
parse(value, options?) |
{ e164, national, international, country, calling_code, extension }, or null when the number is not valid |
isValid(value, options?) |
true when parse() would return a number |
format(value, format?, options?) |
The number as 'e164' (default), 'national' or 'international', or null when it is not valid |
options.default_country is an ISO 3166-1 alpha-2 code and overrides the default set
in the constructor. Without either, the default country is US.
| Input | Read as |
|---|---|
Starts with + |
An international number, exactly as typed: +1 502-555-0123, +44 20 7946 0958 |
No + |
A number of the default country: 502-555-0123, (502) 555-0123, 1 502 555 0123 |
No +, not valid in the default country, 11 digits or more |
An international number without the +: 44 20 7946 0958 |
Anything else is not valid. Short numbers without the + are never reinterpreted as
another country, so 205-555-0142 cannot silently become an Egyptian number.
const sdk = new Rechat.Sdk()
const phone = form.querySelector('[name="phone_number"]')
const phoneNumber = sdk.Leads.Phone.format(phone.value)
if (!phoneNumber) {
showError('Phone number is not correct')
} else {
sdk.Leads.capture(channel, {
first_name,
last_name,
email,
phone_number: phoneNumber,
})
}