Chapter 2 - APIs
Chapter 2 - APIs
Programming
Interface)
Structure of APIs,
status codes, web
services, demo
Introduction
In this chapter we will look at a very essential concept in programming, APIs.
Lots of companies have spent lots of resources and time creating wonderful
solutions (payment, weather, music recognition).
If we want to embed these solutions into our own applications, we have to use the
APIs they created.
Introduction
By the end of this course, you’ll be able to:
• Payment APIs like CamPay permit developers to integrate payment (MoMo and
OM) functionality into their applications without having to rebuild these
payment systems.
• Some other popular APIs include: social media APIs (Facebook, WhatsApp,
Web services
• Web services are specific types of APIs made to function via the web
• HTTP clients use different HTTP methods to request for resources from the
server
• GET: used to retrieve data from the server
403 Forbidden The client does not have permission to access the
requested resource
404 Not Found Server cannot find the requested resource
405 Method Not Allowed The HTTP method used in the request is not supported
for the specified resource.
500 Internal Server Error Server encountered an unexpected condition,
preventing it from fulfilling the request.
503 Service Unavailable Server is unable to handle the request
Data Formats - XML, JSON
XML JSON
• XML is a structured, hierarchical • JSON is a lightweight data format
data format used for exchanging used in RESTful APIs.
information between applications.
• It is more human-readable and
• Commonly used in SOAP-based efficient compared to XML.
APIs.
REST API Creation Guidelines
• REST is an architectural style for modeling distributed systems as a set of
resources.
• Also, REST APIs (based on HTTP) are built around the HTTP actions such as GET,
POST, PUT, DELETE, and PATCH.
• Avoiding chattiness: avoid making the user have to send multiple requests back
and forth. To do this, you can implement endpoints that can add multiple
resources at once, as well as those for bulk delete and bulk updates.
• You can use GraphQL in your API calls without relying on the server-side
application to define the request.
• GraphQL vs REST
• They both allow you to create, update and delete data via API. APIs developed
with REST are known as RESTful APIs while those with GraphQL are GraphQL
APIs.
• Both are stateless, so the server does not save response history between
requests
GraphQL
• GraphQL vs REST
• REST and GraphQL both design their data interchange around resources. Each
resource has its own unique identifier (URI) and a set of operations (HTTP
methods) that the client can perform on it.
• Both REST and GraphQL support similar data formats. JSON is the most popular
format but XML and HTML are sometimes used.
• They both work with any database and programming language, highly
interoperable with any application.
GraphQL
• GraphQL vs REST (differences)
• Overfetching and underfetching: REST APIs always return a whole dataset.
GraphQL emerged as a query-based solution, queries can extract the exact data
in only one API request and response.
• Error handling: GraphQL is a strongly typed API architecture, due to the level of
detail in the schema, the system can automatically identify request errors and
provide useful error messages. REST APIs are weakly typed, and you must build
error handling into the surrounding code.
GraphQL
• When to use GraphQL or REST
• They can be used interchangeably but some use cases are a better fit for one or
the other
• You have data and operations that all clients use similarly
• This article from Amazon Web Services goes into more detail into this.
Example of an API
• A simple REST API for user management:
• Endpoint: https://api.example.com/users
• Response
Example of an API
PRACTICAL:
Interacting with
a Payment API