Managing API Keys for Development, Staging, and Production
Create and manage multiple Township Canada API keys for different environments. Naming conventions, key rotation, environment variables, and CI/CD setup.
Most API integrations span multiple environments-local development, a staging server, and production. Using a single API key across all three creates problems: one accidental revocation takes everything down, you can't track which environment is generating traffic, and debugging becomes harder when all requests look identical in the logs.

Each Township Canada API key is its own paid subscription: one API product (Search, Autocomplete, Batch, Maps, or a parcel intelligence API) on one tier, with its own monthly quota and its own bill. There is no free key. You can hold several keys on the same product, for example one per environment, and each one is billed separately. Weigh that cost when you decide how many environments need a dedicated key: a common split is one key for production and one shared by development, staging, and CI.
Creating API keys
API keys are managed from the Developer Portal. To create a new key:
- Go to API in the main navigation
- Click Add Key and choose the API product
- Enter a descriptive name (see naming conventions below), then pick a tier and monthly or annual billing
- Click Create Key and complete checkout
- Once payment goes through, the key appears in the API keys table
If you already pay for a key on the same product and tier, the portal asks you to confirm, because the new key is billed as its own subscription.
Each key shows its name, tier, usage, and last-used time, and you can copy the full key from the table at any time. Keys can be revoked individually without affecting any other key on your account.
Naming conventions
A consistent naming pattern makes it easy to identify keys at a glance, especially once you have several. Use the format:
{Environment} - {Project} {Version}
Examples:
| Key name | What it's for |
|---|---|
Production - Well Mapper v2 | Live production traffic for version 2 of your app |
Staging - Well Mapper v2 | Pre-release testing against real data |
Dev - Local Testing | Your local development environment |
CI - Well Mapper | Automated tests in your CI pipeline |
Dev - Jane Smith | A team member's personal development key |
Avoid generic names like "API Key 1" or "Test Key". When you're reviewing keys six months from now, a descriptive name tells you exactly what to keep and what to revoke.
Environment variable setup
Never hardcode an API key in your source code. Use environment variables so keys stay out of version control.
Node.js
Create a .env file in your project root:
TOWNSHIP_CANADA_API_KEY=tc_live_your_key_here
Load and use it in your application:
// Make sure dotenv is loaded early (e.g., in your entry file)
require("dotenv").config();
const response = await fetch(
"https://developer.townshipcanada.com/search/legal-location?location=NW-36-42-3-W5",
{
headers: {
"X-API-Key": process.env.TOWNSHIP_CANADA_API_KEY
}
}
);
For the Maps API tiles, pass the key as a query parameter instead:
const tileUrl = `https://maps.townshipcanada.com/ab/twp/{z}/{x}/{y}.mvt?api_key=${process.env.TOWNSHIP_CANADA_API_KEY}`;
Python
Create a .env file:
TOWNSHIP_CANADA_API_KEY=tc_live_your_key_here
Load it using python-dotenv:
import os
from dotenv import load_dotenv
import requests
load_dotenv()
api_key = os.environ.get('TOWNSHIP_CANADA_API_KEY')
response = requests.get(
'https://developer.townshipcanada.com/search/legal-location',
headers={'X-API-Key': api_key},
params={'location': '10-15-23-4-W4'}
)
Keeping keys out of git
Add .env to your .gitignore file:
# .gitignore
.env
.env.local
.env.*.local
Commit a .env.example file with placeholder values instead. Other developers on your team know what variables to set without seeing real credentials:
# .env.example
TOWNSHIP_CANADA_API_KEY=your_api_key_here
Key rotation without downtime
There are two ways to rotate a key, and they trade cost against downtime.
Regenerate in place (no extra cost). Click Regenerate key on the key's row. The old value stops working right away and the new value takes its place on the same subscription, tier, and quota. Have the new value ready to deploy so the gap between regenerating and deploying is as short as possible, ideally during a quiet period.
Overlap with a second key (no downtime). If a production service cannot tolerate even a short gap:
- Add a new key on the same product and tier (this is a second, separately billed subscription)
- Update your deployment - set the new key value in your environment variables or secrets manager
- Deploy and verify - confirm requests are succeeding with the new key in your application logs
- Revoke the old key - only after confirming the new key is working. Revoking cancels the old key's subscription
Never revoke the old key before confirming the new one works. A failed deploy with no fallback means downtime.
CI/CD secrets
Automated pipelines need API keys without human involvement. Store keys in your CI provider's secrets store, not in configuration files.
GitHub Actions
Add your API key as a repository secret:
- Go to Settings → Secrets and variables → Actions in your GitHub repository
- Click New repository secret
- Name it
TOWNSHIP_CANADA_API_KEYand paste your CI key value - Click Add secret
Reference it in your workflow file:
# .github/workflows/test.yml
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run integration tests
env:
TOWNSHIP_CANADA_API_KEY: ${{ secrets.TOWNSHIP_CANADA_API_KEY }}
run: npm test
The same pattern applies to other CI platforms - GitLab CI uses project variables, Bitbucket Pipelines uses repository variables, and CircleCI uses environment variables in project settings. The key principle is the same: the secret is stored in the platform, referenced by name in the config file, and never written to disk or logs.
Team key management
API keys belong to the account that bought them. The Developer Portal lists only your own keys, and team admins do not see or manage keys that other members bought. Keep your own inventory of which keys exist, who holds them, and what they are for.
If your team uses a shared integration (for example, a company-wide internal tool), buy that key from a shared or admin account rather than a personal one. That way the key and its subscription don't depend on one person's account staying active.
Security practices
A few habits that prevent the most common problems:
Don't commit keys. The .gitignore pattern above covers most cases. For extra protection, consider a tool like git-secrets that scans commits for credential patterns before they're pushed.
Use separate keys for separate projects. If Project A's key is ever compromised, you revoke that key without touching Project B. It also makes usage monitoring cleaner - each key's activity in the logs maps to exactly one project.
Rotate keys periodically. There's no hard rule on frequency, but a yearly rotation for production keys is a reasonable baseline. After a team member departure, rotate any keys they had access to.
Revoke unused keys. Old keys from completed projects or former team members are attack surface with no benefit, and each one is still billed. If a key hasn't been used in 90 days and you don't recognize its purpose, revoke it, which also cancels its subscription.
Treat keys like passwords. Don't paste them into chat messages, emails, or support tickets. If you need to share a key temporarily, do it through a password manager or secrets vault.
Related guides
- API Integration - Get started with the Township Canada API
- Batch API Guide - Process multiple records in a single request
- Autocomplete API Guide - Add LLD search suggestions to your app
- Account Management - Manage subscriptions and billing
- Pricing - API plan tiers and request quotas
Related Guides
Agriculture and Energy APIs - Wells, Soil and Tenure by Legal Land Description
Query AER wells, pipelines, Crown tenure, soil and crop data by Canadian legal land description. Reports, filters, paging and vector tiles.
Legal Land Description API Integration Guide
Integrate legal land description APIs into your applications. Convert LLDs to coordinates, add autocomplete search, process batch records, and display DLS/NTS grid maps. REST API with JSON responses.
API Troubleshooting & Best Practices
Fix common Township Canada API errors including Forbidden responses, CORS issues, rate limiting, and empty results. Includes debugging tips and integration patterns.