API Contracts define the formal agreement between an API provider and its consumers, specifying how they interact, what data they exchange, and how to handle success or failure. Contracts describe API behavior, including request formats, HTTP methods, and expected responses. For example, the API contract for retrieving a Cash Card would specify that a GET
request to /cashcards/{id}
returns the card's details in JSON format if the request is successful (200 OK), or an error if not (e.g., 401 Unauthorized or 404 Not Found).
Why API Contracts Matter: API contracts help both the provider and consumer understand how to interact with each other and can be turned into automated tests to ensure everything works as expected. These contracts are essential in ensuring consistent API behavior.
JSON (JavaScript Object Notation) is a lightweight and widely-used data interchange format for APIs. JSON is human-readable, compact, and works well with most modern programming languages. It's preferred over XML for web-based APIs due to its simplicity and performance.
GET
request to /cashcards/{id}
returns JSON data like { "id": 99, "amount": 123.45 }
./cashcards/{id}
로 GET
요청을 보내면 JSON 형식으로 카드를 반환하며, 성공 시 200 OK, 오류 시 401 Unauthorized 또는 404 Not Found 상태 코드를 반환합니다.API 계약의 중요성: API 계약은 제공자와 소비자가 서로 어떻게 상호작용해야 하는지를 명확히 하며, 자동화된 테스트로 쉽게 전환할 수 있어 안정적인 API 작동을 보장합니다.
/cashcards/{id}
에 대한 GET
요청으로 { "id": 99, "amount": 123.45 }
같은 JSON 데이터를 반환.