liveBook · Manning

Contract

As explained in Django in Action

An API is a defined interface through which software components exchange requests, responses, or data.

An API is a defined interface that lets software systems access a project’s data and logic without connecting directly to its database. It can support mobile applications, single-page applications, other computers, and traditional web pages. section 11.1 section 1.2.1

A REST-style API commonly organizes data and actions around:

  • URLs that identify resources, treated as the system’s nouns. section 11.5
  • HTTP methods that act as verbs: GET fetches, POST creates or modifies, PUT and PATCH update, and DELETE removes. section 11.5
  • Payloads commonly formatted as JSON or XML; clients need to know both the method and payload format. section 11.5

In Django, an API endpoint is a view that inspects the HTTP method and performs the requested action. Django Ninja reduces the plumbing: decorators turn view functions into endpoints, Schema objects translate JSON, and ModelSchema can derive a schema from a Django model. A central NinjaAPI object registers routes, while routers group related endpoints. section 11.1.2 section 11.5

Versioning a breaking change

Suppose an API removes an object or adds a required field. Put a new version number in the URL, maintain the older URL for a while, and let existing clients migrate without breaking. section 11.1.2

Figure 11.1 Mixing web and API interfaces allows Django to be a backend for multiple types of devices
figure

Compare how one Django backend can serve server-rendered pages, mobile applications, and JavaScript single-page interfaces. section 11.1

How Django in Action covers it

How deep it goes
Explains REST methods, payloads, versioning, Django views, Ninja routes, routers, and schemas.
Examples
Uses mobile apps, SPAs, JSON and XML payloads, HTTP CRUD methods, Django Ninja endpoints, and versioned URLs.
What it assumes
Assumes Django views, HTTP methods, and project data while connecting those ideas to API endpoints.
What you can do after
Build REST APIs in Django using views or Django Ninja, including routes, schemas, CRUD actions, and versions.
How it connects
Ties APIs to REST, HTTP, CRUD, JSON, Django views, Django Ninja, mobile applications, and single-page applications.

Also explained in

Compare how 44 books explain contract →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.