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_toimport 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
409with the error codeprompt_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
countriesruns in the brand's default country for prompts. A line with nointentgetsother. - New prompts are active at once, and Lumirank asks them in the brand's next daily run.
Export every answer to a warehouse
Read every answer, mention, and citation 1 week at a time, and save them as 3 CSV files that you can load into a data warehouse.
Get your brand GET
Returns the brand that the API key belongs to: its name, domains, countries, engines, prompt counts, and the time of its newest answer. It also names the key that made the request. Use it to check that a key works, and to read the ids that other requests take. `competitor_id` is the id of your own brand in `/v1/mentions` and in the brands report.