Lead generation automation (Apollo → Apify → OpenAI → CSV)
Overview
This service runs the code_crafter/apollo-io-scraper Apify actor to pull leads from Apollo.io search URLs, enriches each lead with a personalized welcome message using OpenAI, and returns a downloadable CSV. Designed to deploy on Railway.
Pseudocode
- POST /leads with input { searchUrls, maxPages?, maxResults? }
- Validate input and environment (APIFY_TOKEN, OPENAI_API_KEY)
- Start Apify actor run
code_crafter/apollo-io-scraperwith { searchUrls, maxPages, maxResults } - Poll run until Succeeded, then fetch dataset items
- For each lead, build a compact profile { company, industry, website, contact }
- In batches with limited concurrency, call OpenAI to generate a short, polite, tailored welcome message
- Merge generated message into each lead
- Convert to CSV (selected columns + message)
- Return CSV as the HTTP response attachment
Security
- Do not hardcode secrets. Configure via environment variables:
- APIFY_TOKEN
- OPENAI_API_KEY
- Avoid logging PII or secrets. Logs are minimal and redact secrets.
Local Development
- Node 20+ recommended
- Install deps: npm install
- Copy env and fill in values: cp .env.example .env
- Start dev server: npm run dev
- Test request (replace with your Apollo search URL):
curl -X POST http://localhost:3000/leads
-H 'Content-Type: application/json'
-d '{"searchUrls": ["https://app.apollo.io/#/people?sortByField=person_name&sortAscending=true&prospected_by_current_team=true&..."], "maxPages": 1, "maxResults": 100}'
-o leads.csv
Deploy on Railway
- Create a new Railway project and connect this repo
- Add environment variables in Railway:
- APIFY_TOKEN
- OPENAI_API_KEY
- PORT (optional; defaults to 3000)
- Deploy. Railway will run
npm run start.
Notes
- Long-running scrapes: For large result sets, the request may take time. Start with small
maxResultsand increase gradually. - Rate limits: OpenAI calls use limited concurrency to respect rate limits. Adjust via
OPENAI_CONCURRENCY.