Skip to main content
This example demonstrates how to take a list of raw lead notes and enrich them with structured data like industry, company size, and recommended follow-up actions.

The Scenario

You have a list of potential leads with unstructured notes. You want to turn this:
“Signed up after webinar, growth-stage SaaS, 80 employees. Wants a live demo next week.”
Into this:
{
  "company_name": "Acme Analytics",
  "industry": "Software",
  "company_size": "Mid-market",
  "recommended_follow_up": "Call"
}

Implementation

1. Define the Schema

First, we define the structure of the data we want to extract.
import { z } from 'zod';

const LeadInsights = z.object({
  company_name: z.string(),
  industry: z.string(),
  company_size: z.enum(['SMB', 'Mid-market', 'Enterprise']),
  recommended_follow_up: z.enum(['Call', 'Email', 'Event']),
});

2. Create the Function

We use createArrayFn because we want to process multiple leads at once.
import { Nua } from 'nuabase';

const nua = new Nua({ apiKey: 'YOUR_API_KEY' });

const enrichLeads = nua.createArrayFn({
  prompt: `Summarize each inbound lead by extracting company_name, industry, 
           company_size bucket (SMB, Mid-market, Enterprise), and the 
           recommended_follow_up channel (Call, Email, or Event) based on their notes.`,
  output: {
    name: 'leadInsights',
    schema: LeadInsights,
  },
});

3. Prepare Data

Each row needs a unique identifier (primary key).
const rows = [
  {
    leadId: 'lead-101',
    name: 'Acme Analytics',
    notes: 'Signed up after webinar, growth-stage SaaS, 80 employees. Wants a live demo next week.',
  },
  {
    leadId: 'lead-102',
    name: 'Bright Logistics',
    notes: 'Regional freight operator asking for pricing via email. 12 depots across the Midwest.',
  },
  {
    leadId: 'lead-103',
    name: 'Nimbus Retail',
    notes: 'Enterprise retailer. Mentioned they will send the procurement team to our booth at NRF.',
  },
];

4. Execute and Handle Results

// 'leadId' is the primary key field
const response = await enrichLeads(rows, 'leadId');

if (response.isError) {
  console.error(response.error);
  return;
}

// Iterate through the enriched results
response.data.forEach(({ leadId, leadInsights }) => {
  console.log(`[${leadId}] ${leadInsights.company_name}: ${leadInsights.recommended_follow_up}`);
});

/* Output:
[lead-101] Acme Analytics: Call
[lead-102] Bright Logistics: Email
[lead-103] Nimbus Retail: Event
*/

Why this is powerful

  1. Structured Data: You didn’t have to write regex or complex parsing logic.
  2. Type Safety: The leadInsights object is fully typed in your IDE.
  3. Caching: If you run this again with the same leads, Nuabase returns the cached results instantly for free.