API guides
Pull reported spend for a date range
Steps and code that copy the reported spend of each campaign and day from the Prescient GraphQL API, one page at a time.
To copy reported spend, send the reportedMetrics query and follow the cursor until no page is left. Each row is the spend of one campaign on one day.
How do you pull the rows?
Section titled “How do you pull the rows?”-
Choose the date range with
startDateandendDate, in theYYYY-MM-DDformat. Both dates are in the range. If you leave them out, the range is the last 30 days. -
Send the query with no
aftervalue.Query query ReportedMetrics($startDate: ISO8601Date$endDate: ISO8601Date$after: BigInt) {reportedMetrics(startDate: $startDate, endDate: $endDate, after: $after) {data {reportedDatechannelNamecampaignIdcampaignNamespend}pageInfo {endCursorhasNextPage}}}Variables {"startDate": "2026-01-01","endDate": "2026-01-31","after": null} -
If
pageInfo.hasNextPageistrue, send the same query withafterset topageInfo.endCursor. -
Stop when
hasNextPageisfalse. The last page can have no rows. See Pagination.
What does the full loop look like?
Section titled “What does the full loop look like?”const QUERY = ` query ReportedMetrics($startDate: ISO8601Date, $endDate: ISO8601Date, $after: BigInt) { reportedMetrics(startDate: $startDate, endDate: $endDate, after: $after) { data { reportedDate channelName campaignId campaignName spend } pageInfo { endCursor hasNextPage } } }`;
async function pullReportedSpend(startDate, endDate) { const rows = []; let after = null; do { const response = await fetch('https://api.prescient-ai.io/graphql', { method: 'POST', headers: { Authorization: `apikey ${process.env.PRESCIENT_API_KEY}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ query: QUERY, variables: { startDate, endDate, after } }), }); const { data, errors } = await response.json(); if (errors) throw new Error(errors[0].message);
const page = data.reportedMetrics; rows.push(...page.data); after = page.pageInfo.hasNextPage ? page.pageInfo.endCursor : null; } while (after !== null); return rows;}import osimport requests
QUERY = """query ReportedMetrics($startDate: ISO8601Date, $endDate: ISO8601Date, $after: BigInt) { reportedMetrics(startDate: $startDate, endDate: $endDate, after: $after) { data { reportedDate channelName campaignId campaignName spend } pageInfo { endCursor hasNextPage } }}"""
def pull_reported_spend(start_date, end_date): rows = [] after = None while True: response = requests.post( "https://api.prescient-ai.io/graphql", headers={"Authorization": f"apikey {os.environ['PRESCIENT_API_KEY']}"}, json={ "query": QUERY, "variables": {"startDate": start_date, "endDate": end_date, "after": after}, }, ) body = response.json() if body.get("errors"): raise RuntimeError(body["errors"][0]["message"])
page = body["data"]["reportedMetrics"] rows.extend(page["data"]) if not page["pageInfo"]["hasNextPage"]: return rows after = page["pageInfo"]["endCursor"]Which rows does the query return?
Section titled “Which rows does the query return?”- The query returns a row only for a campaign and day with spend above zero.
campaignNamecan benull.- To get the rows of some campaigns, set
campaignIdsto a list of campaign IDs. - If your key has a channel scope, the query returns only the channels in that scope. See Authentication.
To learn how reported figures differ from modeled figures, see Modeled and reported figures.