import os, requests
from requests.auth import HTTPBasicAuth
CLOSE_KEY = os.environ["CLOSE_API_KEY"]
CLOSE_AUTH = HTTPBasicAuth(CLOSE_KEY, "")
MAVERA_KEY = os.environ["MAVERA_API_KEY"]
def close_get(path, params=None):
r = requests.get(f"https://api.close.com/api/v1{path}", auth=CLOSE_AUTH, params=params or {})
r.raise_for_status()
return r.json()
# 1. List Smart Views
views = close_get("/saved_search/", {"_type": "lead"}).get("data", [])
print("Smart Views:")
for v in views:
print(f" - {v['name']} (id: {v['id']})")
target = views[0] if views else None
if not target:
print("No Smart Views found.")
exit()
# 2. Fetch matching leads
leads = close_get("/lead/", {"query": target.get("query", ""), "_limit": 50,
"_fields": "id,display_name,contacts,custom,description"}).get("data", [])
print(f"\nUsing: {target['name']} ({len(leads)} leads)")
# 3. Build segment description
companies = [l["display_name"] for l in leads[:5]]
titles = list({c["title"] for l in leads for c in l.get("contacts", []) if c.get("title")})[:5]
segment_desc = (
f"Segment: '{target['name']}'\nCompanies: {', '.join(companies)}\n"
f"Titles: {', '.join(titles)}\nCount: {len(leads)}"
)
# 4. Generate outreach content
gen = requests.post(
"https://app.mavera.io/api/v1/generations",
headers={"Authorization": f"Bearer {MAVERA_KEY}"},
json={"prompt": (
f"Write a cold email for this B2B segment. Under 150 words, soft CTA.\n\n"
f"{segment_desc}\n\n"
f"Write 3 variants: (1) Pain-point led, (2) Social proof, (3) Curiosity/question led"
)},
)
gen.raise_for_status()
print(f"\n{'='*50}\nGenerated content for: {target['name']}\n{'='*50}")
print(gen.json().get("content", gen.json().get("text", "")))
# 5. LinkedIn variant
li = requests.post(
"https://app.mavera.io/api/v1/generations",
headers={"Authorization": f"Bearer {MAVERA_KEY}"},
json={"prompt": f"Write a LinkedIn connection request (under 300 chars) for this segment.\n\n{segment_desc}"},
)
li.raise_for_status()
print(f"\nLinkedIn message:\n{li.json().get('content', li.json().get('text', ''))}")