Foyer API · Recipe
Auto-approve callers from my CRM
Your clients should not have to explain themselves to a screener. This script reads your CRM's contact export and approves each number, so those callers ring straight through.
You need: Concierge, a key with contacts:read and contacts:write, and a CSV export from your CRM with a name and a phone number column. Most CRMs can export contacts as CSV; you can also read from your CRM's own API instead of the file.
How it works
- Check that your plan allows changes:
GET /v1/mereturnsaccess.writes_allowed. - Validate and deduplicate the entire export locally; the preview lists every intended approval.
- Only with
--apply, for each reviewed number callPOST /v1/contacts/approvewith the number and name. It is the same as choosing Through on a contact in the app.
Approving a number also clears a block on it. Review the export first so you do not let through someone you blocked on purpose.
The script
import argparse
import csv
import os
import re
import time
import requests
API = "https://api.hifoyer.com/v1"
def review_rows(rows):
"""Validate a whole export before applying anything; require country codes."""
proposed = {}
for line, row in enumerate(rows, 2):
raw = row.get("Phone", "").strip()
if not raw:
continue
number = re.sub(r"[ ()-]", "", raw)
if not re.fullmatch(r"\+[1-9][0-9]{7,14}", number):
raise ValueError(f"Row {line}: use +country-code format for {raw!r}")
name = row.get("Name", "").strip()
if number in proposed and proposed[number] != name:
raise ValueError(f"Row {line}: conflicting names for {number}")
proposed[number] = name
return proposed
def main():
parser = argparse.ArgumentParser()
parser.add_argument("csv_path")
parser.add_argument("--apply", action="store_true")
args = parser.parse_args()
with open(args.csv_path, newline="", encoding="utf-8-sig") as f:
proposed = review_rows(csv.DictReader(f))
for number, name in proposed.items():
print("Would approve:", number, name or "(keep existing name)")
print(f"{len(proposed)} unique numbers. Approvals can clear blocks.")
if not args.apply:
print("Dry run only. Review, then rerun with --apply to make changes.")
return
headers = {"Authorization": f"Bearer {os.environ['FOYER_API_KEY']}"}
def call(method, path, **kwargs):
for attempt in range(5):
resp = requests.request(method, f"{API}{path}", headers=headers, timeout=30, **kwargs)
if resp.status_code == 429 and attempt < 4:
time.sleep(int(resp.headers.get("Retry-After", "5")))
continue
resp.raise_for_status()
return resp.json()
if not call("GET", "/me")["data"]["access"]["writes_allowed"]:
raise SystemExit("Approvals require a plan that allows changes.")
for number, name in proposed.items():
payload = {"number": number, **({"name": name} if name else {})}
call("POST", "/contacts/approve", json=payload)
print("Approved:", number)
time.sleep(1.1)
if __name__ == "__main__":
main()Change "Phone" and "Name" to your export's column names. This example requires a leading + and country code; it does not guess number formats. Approving a number that is already approved is safe, but a name you send replaces the name Foyer has saved for that number.
Run python3 foyer_approve_from_crm.py crm-export.csv to preview. After review, add --apply to approve those numbers. This is not a transaction: if a request fails, earlier approvals remain applied. It adds approvals and never removes one. Do not schedule it unattended without your own review policy.
Related
- Screen someone again:
POST /v1/contacts/unapprove. - Screen a contact like a stranger for a while:
POST /v1/contacts/appear-unknown. - Decide from a call instead:
POST /v1/calls/{id}/refilewiththrough,screened,review,voicemailorblock.
All are in the endpoint reference.