Lumirank API docs

Import prompts from a CSV

Add prompts from a CSV file with a write key. The script skips prompts you already have, and stops when your plan's limit is reached.

You need a key with Read & write access. Your plan allows a set number of prompts, and each prompt counts once for each of its countries.

The file prompts.csv has 1 prompt per line. Separate the countries with spaces:

text,countries,intent
What is the best CRM for a 5 person sales team?,US GB,comparison
How do I move contacts from spreadsheets to a CRM?,US,how_to
import csv
import os
import time

import requests

API = "https://api.lumirank.ai/v1"
HEADERS = {"Authorization": f"Bearer {os.environ['LUMIRANK_API_KEY']}"}

with open("prompts.csv", newline="") as source:
    for line, row in enumerate(csv.DictReader(source), start=2):
        body = {"text": row["text"], "intent": row.get("intent") or "other"}
        if row.get("countries"):
            body["countries"] = row["countries"].split()
        while True:
            resp = requests.post(f"{API}/prompts", headers=HEADERS, json=body, timeout=30)
            if resp.status_code != 429:
                break
            time.sleep(int(resp.headers.get("Retry-After", "1")))
        if resp.status_code == 201:
            print(f"line {line}: added {resp.json()['id']}")
            continue
        error = resp.json()["error"]
        if error["code"] == "prompt_already_exists":
            print(f"line {line}: skipped, {error['message']}")
            continue
        if error["code"] in ("effective_prompt_quota_exceeded", "onboarding_prompt_cap_reached"):
            raise SystemExit(f"line {line}: plan quota is full: {error['message']}")
        print(f"line {line}: {resp.status_code} {error['code']}: {error['message']}")
  • If the brand already has a prompt with the same text, in any status, the request gets status 409 with the error code prompt_already_exists. Capital letters and extra spaces do not make a text different. So you can run the script twice: the 2nd run skips every line.
  • A line with no countries runs in the brand's default country for prompts. A line with no intent gets other.
  • New prompts are active at once, and Lumirank asks them in the brand's next daily run.