The Civic API provides access to government representatives and division-related data. The old API endpoint used representatives?address= to fetch data, whereas the new API endpoint requires divisionsByAddress?address= for better and updated results. This document details the migration process from the old Civic API to the new Civic API due to the expiration of the old API in April.
Changes in API Endpoint
Old API Format:
“url”: “https://content-civicinfo.googleapis.com/civicinfo/v2/representatives?address=” + address_string + “&alt=json&key={key}&includeOffices=false”
New API Format:
“url”: “https://www.googleapis.com/civicinfo/v2/divisionsByAddress?address=” + address_string + “&alt=json&key={key}&includeOffices=false”
Key Differences
Feature | Old API Endpoint | New API Endpoint |
Base URL | https://content-civicinfo.googleapis.com/civicinfo/v2/ | https://www.googleapis.com/civicinfo/v2/ |
Endpoint | /representatives?address= | /divisionsByAddress?address= |
Purpose | Fetches representatives based on address | Fetches electoral divisions based on address |
API Expiry | Expires in April | Latest version, recommended for future use |
Migration Steps
- Update API Endpoint: Replace /representatives?address= with /divisionsByAddress?address= in all occurrences.
- Modify Request Handling: Ensure that the response handling is adjusted according to the data structure returned by the new API.
- Test API Response: Validate the output of the new API to check if it meets the application’s requirements.
- Deploy and Monitor: Deploy the updated code and monitor for any issues.
Example Code Before and After Migration
Old Code (Before Migration)
const address_string = "1600 Amphitheatre Parkway, Mountain View, CA";
const url = "https://content-civicinfo.googleapis.com/civicinfo/v2/representatives?address=" + address_string + "&alt=json&key=YOUR_API_KEY&includeOffices=false";
fetch(url)
.then(response => response.json())
.then(data => console.log(data));
New Code (After Migration)
const address_string = "1600 Amphitheatre Parkway, Mountain View, CA";
const url = "https://www.googleapis.com/civicinfo/v2/divisionsByAddress?address=" + address_string + "&alt=json&key=YOUR_API_KEY&includeOffices=false";
fetch(url)
.then(response => response.json())
.then(data => console.log(data));
Expected Response Differences
- The new API returns electoral divisions rather than direct representative data.
- The response format may vary slightly; ensure that JSON parsing is updated accordingly.
Conclusion
The migration to the new API ensures continued functionality and access to up-to-date government data. All implementations must be updated before the expiration of the old API in April to avoid service disruptions.