Skip to content
Open Prescient AI
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.

  1. Choose the date range with startDate and endDate, in the YYYY-MM-DD format. Both dates are in the range. If you leave them out, the range is the last 30 days.

  2. Send the query with no after value.

    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
    }
    }
    }
    Variables
    {
    "startDate": "2026-01-01",
    "endDate": "2026-01-31",
    "after": null
    }
  3. If pageInfo.hasNextPage is true, send the same query with after set to pageInfo.endCursor.

  4. Stop when hasNextPage is false. The last page can have no rows. See Pagination.

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;
}
  • The query returns a row only for a campaign and day with spend above zero.
  • campaignName can be null.
  • To get the rows of some campaigns, set campaignIds to 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.