11 APIs

 

This chapter covers

  • Adding API capabilities to your Django project
  • REST API interfaces
  • Using Django Ninja to build APIs

Adding an API to your project creates a way for other systems to access your data, including alternate frontend interfaces, such as mobile devices and single-page applications (SPAs). The most common API protocol on the web is REST, a mechanism built on top of URLs and HTTP calls. The third-party library Django Ninja allows you to build REST-based APIs using the same view functions you’ve used for your pages.

11.1 Why use an API?

When you think about a web-based software system, your first inclination is that your users are people. You shouldn’t forget the people; they’re definitely your primary audience, but sometimes, they aren’t your only users. All that interesting information stored away in your database might also be useful to another piece of software. Like with people, you want to be able to control what other systems have access to, so you can’t just let them attach to your database. By providing an application programming interface (API), your project can be accessed by other systems.

11.1.1 CRUD and REST

11.1.2 Versioning an API

11.2 Building an API with Django Ninja

11.2.1 Serializing objects

11.2.2 Handling arguments

11.2.3 Filtering your listings

11.2.4 Nesting data

11.2.5 Authenticating your API

11.2.6 Creating objects with the API

11.2.7 Updating and deleting with the API

11.3 Other API libraries

11.4 Exercises

Summary