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:

  1. Go to API in the main navigation
  2. Click Add Key and choose the API product
  3. Enter a descriptive name (see naming conventions below), then pick a tier and monthly or annual billing
  4. Click Create Key and complete checkout
  5. 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.

Revoking a key (the delete action) also cancels that key's subscription. To replace a leaked or old credential, use Regenerate key instead: it issues a new key value and keeps the same subscription, tier, and billing period.

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 nameWhat it's for
Production - Well Mapper v2Live production traffic for version 2 of your app
Staging - Well Mapper v2Pre-release testing against real data
Dev - Local TestingYour local development environment
CI - Well MapperAutomated tests in your CI pipeline
Dev - Jane SmithA 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:

  1. Add a new key on the same product and tier (this is a second, separately billed subscription)
  2. Update your deployment - set the new key value in your environment variables or secrets manager
  3. Deploy and verify - confirm requests are succeeding with the new key in your application logs
  4. 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:

  1. Go to Settings → Secrets and variables → Actions in your GitHub repository
  2. Click New repository secret
  3. Name it TOWNSHIP_CANADA_API_KEY and paste your CI key value
  4. 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.