Walkthrough: Design a Payments API

Topics Covered

Clarify and Model the Payments Domain

Step one: clarify the consumers and use cases

The non functional requirements that drive the design

Step two: model the resources

Relationships and identifiers

Money representation

Checking the model against the use cases

Design a payments API is one of the most common prompts in API design rounds, and one of the most revealing. It looks like a CRUD problem with a charge resource, but nearly every interesting failure in distributed systems shows up in it: retries that could double charge, outcomes that arrive minutes or days later, money that must never be rounded, state that must never go backwards. An interviewer can probe any of those, so a strong answer walks the six step framework from lesson one deliberately and goes deep where payments are genuinely hard.

This lesson works through the whole design as you would in the room. Each section matches one or two steps of the framework: clarify and model, choose the style and write the core endpoints, define the state machine, handle idempotency and refunds, then cover webhooks, errors and evolution. The examples are original and simplified, though they follow patterns common to real payment providers.

Step one: clarify the consumers and use cases

Start by asking who calls the API. For a payments provider there are usually three consumers. Merchant backends call it with secret keys to create and manage payments. Merchant frontends, browsers and mobile apps, collect card details and confirm payments with a limited publishable key, because the merchant's server should not touch raw card numbers if it can avoid it. Merchant staff and finance teams use dashboards and exports, which call the same API. Naming all three early lets you justify the split between secret and publishable keys later.

Then ask about scope. Are we handling one time card payments only, or also saved cards, subscriptions, bank transfers and wallets? A good scoped answer for a forty five minute round is: one time payments and saved cards, with card and one alternative method, authorize and capture, full and partial refunds, and webhooks for asynchronous outcomes. Subscriptions, disputes and payouts are out of scope but should be named so the interviewer knows you thought of them. Ask about currencies: multi currency is common, and it changes how you represent money.

The non functional requirements that drive the design

Payments have an unusual priority order. Correctness beats latency: a slow payment is annoying, a double charge is a support ticket and possibly a regulatory issue. Durability is absolute: once you tell a merchant a payment succeeded, that fact must survive any failure. Outcomes are often asynchronous: a card can require the customer to authenticate with their bank, a bank transfer settles days later, and a dispute can arrive weeks later. Security and compliance matter: raw card data puts systems in the scope of the card industry's security standard, so the design should minimise where card numbers flow.

State these explicitly, because each one earns a design choice later. No double charges leads to idempotency keys on every creating call. Asynchronous outcomes lead to a state machine and webhooks. Card data minimisation leads to tokenised payment methods and client side confirmation. Durability leads to a write ahead record of every call to the card processor.

A quick scale estimate is still worth doing, even though payments are rarely bottlenecked by raw throughput. Suppose the platform handles ten million payments a day. That is a little over a hundred per second on average, with peaks perhaps ten times higher on sale days. The numbers tell you the database can be a conventional strongly consistent store, sharded by merchant if needed, and that the hard parts are correctness and external dependencies, not request volume. Say that conclusion out loud, because it justifies spending your deep dive time on state and idempotency rather than on caching.

Step two: model the resources

With the requirements set, name the resources. A good set is small.

Model the domain as a few resources with clear ownership before writing a single endpoint.

A customer represents a buyer the merchant wants to remember. A payment method is a tokenised instrument, such as a card or bank account, created from card details collected on the client, and optionally attached to a customer for reuse. A payment intent represents the merchant's intention to collect a specific amount, and tracks that attempt from creation to success or cancellation. A refund returns some or all of a captured payment and has its own lifecycle. An event records every state change and feeds webhooks and the events feed from lesson nine.

Why a payment intent rather than a simple charge? A charge that is created and completed in one call works only if the payment finishes synchronously. Modern card payments often need the customer to authenticate with their bank in the middle, and some methods take days. An intent is a durable object that survives those pauses: you create it, confirm it, it may wait for customer action, it processes, and it eventually reaches a terminal state. Each underlying attempt to charge the card can be recorded as a child attempt resource, so a retry with a different card after a decline still belongs to the same intent. Explaining this choice is one of the best signals you can give in the modelling step.

Relationships and identifiers

Use opaque, prefixed string identifiers such as pi_3Kx9, pm_card_42, re_1Ab and cus_7Q. Prefixes make logs and support tickets readable and catch mistakes, such as passing a refund id where a payment id is expected. Never use sequential integers, which leak volume and invite enumeration. All resources belong to a merchant account, which is implied by the API key rather than appearing in the URL.

Refunds are naturally a sub resource of a payment, but refunds also need to be listed across all payments for reconciliation. A common compromise is a top level refunds collection with a payment_intent field, plus filtering: GET /v1/refunds?payment_intent=pi_3Kx9. This avoids deep nesting while keeping the relationship clear, which is the trade-off lesson two discussed.

Money representation

Now the detail that trips up many candidates. Never represent money as a floating point number. Binary floats cannot represent 0.10 exactly, so sums drift and comparisons fail. Represent amounts as integers in the currency's minor unit, with an ISO 4217 currency code alongside.

Store money as an integer in the currency's minor unit plus an ISO currency code, never as a float.
json
{"amount": 4200, "currency": "usd"}
{"amount": 500, "currency": "jpy"}
{"amount": 1500, "currency": "kwd"}

The minor unit differs by currency. US dollars and euros have two decimal places, so 4200 means 42.00. Japanese yen has none, so 500 means 500 yen. Some currencies, such as the Kuwaiti dinar, use three, so 1500 means 1.500 dinar. Your API documentation must state the exponent per currency, and your SDKs should provide helpers to convert. Amounts should be positive integers, with direction expressed by the resource, a payment or a refund, rather than by a sign. Currency conversion, if offered, should appear as explicit fields such as a settlement amount, settlement currency and exchange rate, never silently.

Saying all of this in about a minute, with the yen example, signals real payments experience and closes off a whole class of follow up questions.

Checking the model against the use cases

Before moving on, test the model against the use cases you agreed, out loud and quickly. A customer pays once with a new card: create an intent, attach a new payment method, confirm. A returning customer pays with a saved card: the payment method is already attached to the customer, so the merchant passes its id. The merchant ships later and charges then: manual capture on the intent. A customer returns an item: a refund against the intent. Finance reconciles the month: list intents and refunds by time, plus the events feed. A bank transfer settles after three days: the intent waits in processing and a webhook announces the result. If every use case maps to resources you already have, the model is complete enough to proceed. If one does not, such as a subscription renewal, either add a resource or confirm it is out of scope.

This check takes under a minute and gives the interviewer a reason to trust the model before you spend twenty minutes building on it. It also catches a common gap: candidates who model only the happy path discover in the deep dive that they have nowhere to put a declined attempt or a failed refund.

Why an intent, not a charge

A one shot charge only works when payment finishes inside the request. A payment intent is a durable object that can wait for customer authentication, retry with another card and settle later, which is how modern payments actually behave.