KatInterview

Client for migrating records between REST APIs with OAuth

System DesignModel answer
MediumModel answerSystem Designoauthapi-integrationdata-migration

Last updated

Write a CLI tool that authenticates via OAuth 2.0, fetches paginated data from one API, transforms it, and posts it to another with checksums and idempotency.

You need to design and implement a command-line tool that migrates user records from an old REST API to a new one. The tool must authenticate using OAuth 2.0, handle pagination, transform data formats, ensure data integrity with checksums, and be resilient to failures.

Requirements

Your tool should:

  1. Authenticate using OAuth 2.0 Client Credentials flow
  2. Fetch records from the source API (supports pagination)
  3. Transform each record according to specified mapping rules
  4. Upload transformed records to the destination API
  5. Compute integrity checksums for each upload
  6. Handle failures gracefully with retries and resumability

API Specifications

Authentication Endpoint

POST https://auth.service.io/token
Content-Type: application/json

Request body:
{
  "client_id": "your_client_id",
  "client_secret": "your_secret",
  "grant_type": "client_credentials"
}

Response:
{
  "access_token": "eyJhbG...",
  "expires_in": 3600
}

Source API (Legacy System)

GET https://old-api.service.io/users?page=<n>&limit=<k>
Authorization: Bearer <token>

Response:
{
  "users": [
    {
      "user_id": 42,
      "given_name": "Grace",
      "family_name": "Hopper",
      "contact_email": "[email protected]",
      "registered_ts": 1609459200,
      "account_status": "ACTIVE"
    }
  ],
  "next_page": 2
}

When next_page is null, you've reached the end.

Destination API (New System)

POST https://new-api.service.io/users
Authorization: Bearer <token>
Content-Type: application/json
X-Integrity-Hash: <sha256_hex>
Idempotency-Key: <unique_stable_id>

Request body (one record):
{
  "source_id": 42,
  "name": "Grace Hopper",
  "email": "[email protected]",
  "registered_at": "2021-01-01T00:00:00Z",
  "active": true
}

Data Transformation Rules

Map each source record to the destination format:

  • source_id ← user_id
  • name ← given_name + " " + family_name (normalize whitespace)
  • email ← lowercase(contact_email)
  • registered_at ← convert registered_ts (Unix epoch) to ISO 8601 UTC string
  • active ← true if account_status == "ACTIVE", otherwise false

Checksum Calculation

The X-Integrity-Hash header must contain the SHA-256 hash of the exact JSON bytes you send. Use a canonical JSON representation (sorted keys, no extra whitespace) to ensure consistency across retries.

Operational Requirements

  • Token management: Refresh tokens before they expire; handle 401 responses by re-authenticating
  • Retry logic: Retry transient failures (network errors, 429, 5xx) with exponential backoff
  • Idempotency: Use stable idempotency keys (derived from user_id) so rerunning the tool doesn't create duplicates
  • Resumability: Track progress so the tool can resume after crashes without reprocessing everything
  • Logging: Report progress, errors, and any records that fail validation

Deliverables

Provide:

  1. Design document (1-2 pages) explaining your architecture, data flow, error handling strategy, and how you ensure idempotency
  2. Implementation in your preferred language
  3. Test strategy covering transformation logic, checksum generation, and how you would mock the APIs

Discuss trade-offs in your design choices (e.g., sequential vs. parallel uploads, memory usage, fault tolerance).

Hints

Hint 1

Separate pure transformation functions from I/O so they can be unit tested independently

Hint 2

Manage token lifecycle proactively—refresh before expiration rather than waiting for 401s

Hint 3

Use the source record ID to generate stable idempotency keys that remain the same across retries and restarts