Lesson 2 Api
Lesson 2 Api
LESSON 2
API
The term API is an acronym, and it
stands for “Application
Programming Interface.”
API
► An API (Application Programming Interface) is an interface
between two applications that enables them to communicate
with each other.
► Server
► The HTTP server responds with a status line, including
the message's protocol version and a success or error
code, followed by a MIME-like message containing server
information, entity meta information, and possible entity-
body content.
API Calls
An API key is a unique identifier used to authenticate calls to an API. The key is
made up of a string of letters and numbers that identify the client. (Remember,
this is the application or site making the request.)
Benefits of APIs
1. New data available: an API allows all of the information generated
at the government level to be available to every citizen, not just a
select few.
2. Efficiency: when access is provided to an API, the content
generated can be published automatically and is available for every
channel. It allows it to be shared and distributed more easily.
3. Integration: APIs allow content to be embedded from any site or
application more easily. This guarantees more fluid information
delivery and an integrated user experience.
4. Adaptation: needs change over time and APIs help to anticipate
changes. When working with this technology, data migration is
supported better, and the information is reviewed more closely. In
short, APIs make service provision more flexible.
What are APIs used for
Data sharing.
Any time a program needs to get data from a
third party (e.g., a travel app compiling flight
times from airlines), data can be shared through
an API.
App integrations.
When two digital applications work in conjunction
— HubSpot and Gmail, for example — an API is
likely involved.
What are APIs used for
Embedded content.
To embed a piece of content that is hosted on a
different website — like a YouTube video — a request
is made to the embedded content's owner to retrieve
it.
Internal systems.
APIs aren't only for sharing data externally.
Businesses frequently divide their software
infrastructure into smaller components. These
elements communicate with each other through APIs,
like in a microservice architecture.
Types of APIs
Private APIs. These APIs are only made available to a
company's internal team to boost productivity and
transparency. Developers working for the company can
use these APIs as needed. Third-party developers can’t.
Partner APIs. These APIs are shared externally but only
with those with a business relationship with the API
company. Some businesses use partner APIs because they
want control over who can access their resources and how
they get used.
Open APIs. Open APIs, or public APIs, are available for
external use. While some open APIs are free, others
require a subscription fee, which is often tiered based on
usage.
Benefits of APIs
1. Automation: with APIs, computers rather than people can
manage the work. Through APIs, agencies can update work
flows to make them quicker and more productive.
2. Application: because APIs can access the app components, the
delivery of services and information is more flexible.
3. More scope: with an API an application layer can be created
which can be used to distribute information and services to
new audiences which can be personalized to create custom
user experiences
4. Personalization: through APIs any user or company can
customize the content and services that they use the most.
How to Use an API
1. Select an API.
2. Choose your authorization method.
3. Review the API documentation.
4. Write a request to an endpoint.
5. Connect your app.
We have two new concepts
here:
► protocol
► format
Protocol & Format
► The protocol is the set of rules that defines how applications can interact
with each other
► Format specifies how the data can be sent to and accessed by other
applications.
► The most used protocol is HTTP, which stands for Hypertext Transfer
Protocol, but SOAP, REST and XML-RPC can also be used as means of
communication.
HTTP protocol
► HTTP is the protocol used by web
browsers — referred to as clients —
for communicating with web
servers.
► Method
► The request method tells the server what kind of action the
client wants the server to take. In fact, the method is
commonly referred to as the request "verb.“
► There are four main types of methods:
► GET — which means that you want to get data from the second
application
► POST — you want to create information and add it in the second
application
► PUT — you want to change data in the second application
► Headers
► provide meta-information about a request.
They are a simple list of items like the time
the client sent the request and the size of
the request body.
► Body
► The request body contains the data the
client wants to send the server.
HTTP RESPONSE
200
301
400
500
How to Read an HTTP Response
RESPONSE CODE SAMPLES
200 (OK)
301 (Moved Permanently)
400 (Bad Request)
500 (Internal Server Error)
HTTP GET: How to Read an HTTP Response
HTTP/1.1 200 OK
ETag: "f60e0978bc9c458989815b18ddad6d75"
Last-Modified: Thu, 10 Jan 2013 01:45:22 GMT
Content-Type: application/json
{ "collection":
{
"version" : "1.0",
"href" : "http://www.youtypeitwepostit.com/api/",
"items" : [{ "href" : "http://www.youtypeitwepostit.com/api/messages/21818525390699506",
"data": [
{ "name": "text", "value": "Test." },
{ "name": "date_posted", "value": "2013-04-22T05:33:58.930Z" }
],
"links": []
}
{
How to Read an HTTP Response
The entity-body, sometimes called just the
body
This is a document written in some data
format, which the client is expected to
understand.
If you think of a GET request as a request
for a representation, you can
think of the entity-body as the
representation
How to Read an HTTP Response
► Sample
► scheme_name:hierarchical_part?query#fragment
RESTful Resources
► RESTful service it is very desirable to have all your resources structured by
their hierarchy. These are then specified in the hierarchical part of the
URL.
Sample
http://server.test:8080/order_api/{customer_id}/{order_id}/{book_id}