Skip to main content
All requests use a single endpoint: POST /functions/v1/api with resource: "leads".

Access control

Regular users can only access leads from agents they own. Internal admins can access all leads using adminMode: true and optionally scope to a specific user with userId.

Get lead statistics

Retrieves aggregated statistics about leads, including total count and breakdown by status. Aggregation happens at the database level for optimal performance.
resource
string
required
Must be "leads"
action
string
required
Must be "stats"
data
object

Response

success
boolean
data
object
Leads with a null status or a status outside the four standard values are not counted in any by_status category but are included in total.
const response = await ApiService.invoke<{
  stats: {
    total: number;
    by_status: { new: number; contacted: number; qualified: number; converted: number };
  };
}>({
  resource: "leads",
  action: "stats",
  data: {
    filters: {
      agentIds: ["agent-1", "agent-2"],
      dateRange: "30d",
    },
  },
});

List all leads

Retrieves all leads with full field details and relations.
resource
string
required
Must be "leads"
action
string
required
Must be "all"
data
object

Response

success
boolean
data
object
const response = await ApiService.invoke<{
  leads: Lead[];
  count: number;
  limit: number;
  offset: number;
}>({
  resource: "leads",
  action: "all",
  data: {
    filters: {
      agentId: "agent-123",
      status: "qualified",
      dateRange: "90d",
    },
    limit: 25,
    offset: 0,
  },
});

List leads (summary)

Retrieves leads with basic fields only, optimized for table views. Accepts the same parameters as all.
resource
string
required
Must be "leads"
action
string
required
Must be "list"
data
object
Same parameters as all (filters, pagination, admin options).

Response

Same response structure as all, but each lead contains only summary fields:
data
object
const response = await ApiService.invoke<{
  leads: Lead[];
  count: number;
}>({
  resource: "leads",
  action: "list",
  data: {
    filters: {
      searchTerm: "john",
      priority: "high",
    },
    limit: 25,
  },
});

Get a lead

Retrieves a specific lead by ID with all fields and relations.
resource
string
required
Must be "leads"
action
string
required
Must be "get"
id
string | number
required
The lead ID.

Response

success
boolean
data
object
const response = await ApiService.invoke<{ lead: Lead }>({
  resource: "leads",
  action: "get",
  id: 42,
});

Create a lead

Creates a new lead associated with an agent.
resource
string
required
Must be "leads"
action
string
required
Must be "create"
data
object
required

Response (status 201)

success
boolean
data
object
The user_id on the created lead is resolved in this order: (1) provided user_id parameter, (2) agent’s owning user, (3) current authenticated user.
const response = await ApiService.invoke<{ lead: Lead }>(
  {
    resource: "leads",
    action: "create",
    data: {
      agent_id: "agent-123",
      name: "Jane Doe",
      email: "jane@example.com",
      status: "new",
      priority: "high",
    },
  },
  201
);

Update a lead

Updates an existing lead. This is a partial update — only include the fields you want to change.
resource
string
required
Must be "leads"
action
string
required
Must be "update"
id
string | number
required
The lead ID.
data
object
required
Any combination of lead fields. All fields except id and created_at can be updated. See the create action for the full list of fields.

Response

success
boolean
data
object
const response = await ApiService.invoke<{ lead: Lead }>({
  resource: "leads",
  action: "update",
  id: 42,
  data: {
    status: "qualified",
    priority: "high",
    notes: "Spoke with decision maker, very interested",
  },
});

Delete a lead

Permanently deletes a lead. This is a hard delete with no recovery.
resource
string
required
Must be "leads"
action
string
required
Must be "delete"
id
string | number
required
The lead ID.

Response

success
boolean
data
object
This permanently deletes the lead. The user must have access to the lead’s agent.
const response = await ApiService.invoke<{
  success: boolean;
  message: string;
}>({
  resource: "leads",
  action: "delete",
  id: 42,
});

Lead object

The full lead object returned by all, get, create, and update actions.
id
number
agent_id
string
user_id
string
name
string | null
email
string | null
phone
string | null
status
string | null
priority
string | null
budget
string | null
timeline
string | null
address
object | null
notes
string | null
assigned_to
string | null
conversation_id
number | null
primary_lead_id
number | null
is_decision_maker
boolean | null
preferred_contact_method
string | null
preferred_contact_time
string | null
metadata
object | null
created_at
string
updated_at
string | null
agent
object

Enum values reference

status

ValueDescription
"new"Lead has been captured but not yet contacted
"contacted"Initial outreach has been made
"qualified"Lead has been vetted and meets criteria
"converted"Lead has been successfully converted

priority

ValueDescription
"low"Low urgency
"medium"Standard urgency
"high"Requires immediate attention

preferred_contact_method

ValueDescription
"email"Contact via email
"phone"Contact via phone call
"sms"Contact via text message

Filtering reference

All list-style actions (stats, all, list) accept a filters object with the following fields:
FilterTypeDescription
agentIdstringFilter by a single agent
agentIdsstring[]Filter by multiple agents
statusstringExact match on lead status (not available on stats)
prioritystringExact match on lead priority
searchTermstringPartial match across name, email, and phone
dateRangestringOne of "7d", "30d", "90d", "all"

Admin operations

Admin operations require the internal_admin role. Regular users receive a 403 Forbidden response.

View all leads across all users

const response = await ApiService.invoke<{ leads: Lead[]; count: number }>({
  resource: "leads",
  action: "all",
  data: {
    adminMode: true,
  },
});

View a specific user’s leads

const response = await ApiService.invoke<{ leads: Lead[]; count: number }>({
  resource: "leads",
  action: "list",
  data: {
    adminMode: true,
    userId: "user-456",
  },
});

Create a lead as another user

const response = await ApiService.invoke<{ lead: Lead }>(
  {
    resource: "leads",
    action: "create",
    data: {
      agent_id: "agent-123",
      name: "Jane Doe",
      email: "jane@example.com",
      userId: "user-456",
    },
  },
  201
);

Error responses

StatusCodeDescription
400VALIDATION_ERRORMissing or invalid required parameters
401UNAUTHORIZEDInvalid or missing authentication token
403FORBIDDENUser does not have access to the agent
404NOT_FOUNDLead not found
500INTERNAL_ERRORServer error