Skip to content

Application programming interface

An API is a contract between software components. Learn how names, inputs, outputs, errors, units and versions let quant code use local libraries or remote services.

An application programming interface (API) is a contract describing how one software component can use another: which operations it exposes, what inputs they accept, what results they return and how failures are reported. For quant work, that boundary lets a valuation routine request a quote or call a pricing library without depending on its internal implementation. Correct code needs the meaning of the result as well as its shape.
An API can be a local function, a library's public methods or a remote service interface. It need not use HTTP, JSON or a network. A REST API is one architectural form of remote interface; a quote service can also expose an application contract over WebSocket. Neither term is a synonym for API.

Read the contract beyond the function name

A signature tells you how to call an operation. It rarely tells you everything needed to use the answer safely.

Part of the contractQuestion for a quote interface
Name and signatureWhich operation is public? Which arguments are positional, named or optional?
Inputs and outputsWhat identifies the instrument? Does the result contain one mark, a bid–ask pair or a series?
Error semanticsDoes an unknown symbol raise an exception, return a missing value or produce a documented error object?
Meaning and unitsIs the price per share or per contract? Which currency, quote time and price convention apply?
Version and compatibilityWhich contract is the caller using, and what changes require an update?
These distinctions also apply to local libraries. Python's public API policy, for example, covers argument and return behaviour, side effects and exceptions alongside names and types. A documented interface is broader than a list of callable names.

A local quote interface

This Python example is offline and fictional. Its entire supported instrument universe is the exact string "ABC". The function returns a frozen mark of 100 USD per share as an integer; an unrecognised symbol raises LookupError, and a non-string input raises TypeError. It performs no network request and returns no live market observation.
def quote_usd_per_share(symbol: str) -> int: """Return a fictional frozen mark in whole USD per share; ABC only.""" if not isinstance(symbol, str): raise TypeError("symbol must be a string") if symbol != "ABC": raise LookupError("unknown symbol") return 100 price = quote_usd_per_share("ABC") quantity = 2 # shares value_usd = quantity * price print(f"{quantity} shares at USD {price}/share = USD {value_usd}") # 2 shares at USD 100/share = USD 200 try: quote_usd_per_share("XYZ") except LookupError as exc: print(type(exc).__name__ + ": " + str(exc)) # LookupError: unknown symbol
Two shares at 100 USD per share have a value of USD 200. The unit of measure makes the calculation explicit: shares multiplied by USD per share gives USD. The caller does not inspect a private lookup table or need to know how the mark was stored.

The annotations describe expected types; they do not enforce them at runtime. The explicit checks enforce this example's input restrictions. Returning zero for an unknown symbol would violate its stated error contract and could make a missing quote look like a worthless holding. Callers should handle the documented failure rather than silently substitute a price.

Offline fictional local API: valuation code calls quote_usd_per_share with ABC, receives 100 USD per share and values 2 shares at USD 200. The contract defines the string input, integer output units and LookupError for an unknown symbol. No network call occurs.
Offline fictional local API: valuation code calls quote_usd_per_share with ABC, receives 100 USD per share and values 2 shares at USD 200. The contract defines the string input, integer output units and LookupError for an unknown symbol. No network call occurs.
An API exposes agreed behaviour while the component keeps its lookup logic internal. Fictional fixed marks; the example uses a local function.

This deliberately narrow API supplies a fixed teaching mark, without timestamps, bid–ask information or fees. A live quote contract must define those relevant fields and freshness rules before a valuation can rely on it. Receiving the expected data type does not establish economic validity.

Changes can preserve syntax and break meaning

An internal lookup can change while the public operation keeps its agreed behaviour. But changing the result from USD per share to cents per share would break the caller's interpretation even if the function name, arguments and integer return type stayed identical. Changing a documented exception can also break error handling.

Versioning identifies the contract being used; it does not by itself guarantee compatibility. Read the provider's change and deprecation policy, check units and failure behaviour, and test the integration when upgrading. Python's compatibility policy is an example of one project's explicit rules, rather than a universal guarantee for every API.

For local boundaries, Functions teaches parameters, return values, validation and composition; the full lesson is free with sign-in. For service data, APIs uses offline JSON fixtures to distinguish a successful protocol response from a valid quote. That lesson has a public preview, with the full lesson requiring membership.

Sources

Last updated
  • api
  • interfaces
  • python
  • contracts
  • software design
  • units
← Back to the glossaryBack to top ↑