Optimizing Elections API Requests
Use narrowly scoped requests to request only the data your application needs. Smaller responses are processed and returned more quickly, reducing bandwidth and helping your application handle election updates efficiently. Always Use the Next Request LinkMake one initial request for the races you need. For every subsequent update, use the "nextrequest" URL returned in the JSON response or the link with rel="next" returned in the XML response. The next request link returns a payload only containing races with vote, race call or candidate information changes since the preceding request. When no data has changed, the response contains only the next request link.
For more information, see Receiving Election Updates. Use a Reasonable Polling IntervalThe appropriate request frequency depends on your application’s requirements. However, AP does not recommend making requests more frequently than every few seconds. Polling more frequently, such as every second or multiple times per second, is unlikely to deliver updates materially sooner than polling on a slightly longer cadence, such as every five seconds. After each polling interval, use the latest nextrequest URL returned by the API. Limit Each Request to the Data You NeedRequest One State at a TimeIdeally, specify one state in each request by using the statePostal parameter. If your application requires results from multiple states, make separate state-specific requests. Many smaller requests are typically faster than one request containing multiple states. State-specific requests also produce smaller responses that are easier to process. Request Only the Required Reporting LevelSet level to the least granular value you require:
Reporting unit data can substantially increase the response size, particularly when several races or states are included. Request Live and Certified Results with One CallUse resultsType=b to receive a mixture of live and certified results. Live results will be returned until certified results become available. Request Vote Types Only When NeededDo not specify votetypes=true unless your application uses votes broken down by type, such as electionDayInPerson, advanceInPerson and absenteeMail votes. The default, votetypes=false, returns the cumulative results without the additional vote by type data. Filter the Races in the ResponseFiltering ParametersUse the available filtering parameters to request only relevant races.
Combining FiltersMultiple compatible filters can be combined to narrow a request further; for example: https://api.ap.org/v3/elections/{electionDate}?statePostal=PA&officeID=P,S,H&raceTypeID=G&level=state&resultsType=l&candidateInfo=brief&format=json After the initial response, use its returned next request link for all updates. Reduce Candidate DataUse candidateInfo=brief when your application already stores candidate reference information and needs only candidate IDs, vote, delegate or electoral counts and winner indicators. If a candidate’s reference information changes, a response obtained through the next request link identifies the update and returns the candidate’s full information even when candidateInfo=brief was requested. Use candidateInfo=full only when your application needs all available candidate details. Recommended Request ChecklistBefore sending an Elections API request, confirm that:
|
|
|
|
|
||||
|
|
|
|||||
|
|
||||||