API By Chloe

API - APPLICATION PROGRAMMING INTERFACE

An API is a set of rules that allow programmes to talk to each other. The developer creates the API on the server and it allows clients to talk to it.
They are a technology that allow firms to interact and share information with other firms.
There are three types of API:
1. Private/Internal - Runs on an intranet within an organization.
2. Partner API - Open beyond the organization. Not entirely exposed publicy and will retain some security and data control, however, it produces beneficial b2b relationships.
3. Public API - Expose the same data that a company uses itself - offers something of value to those outside an organization.

REST api

REST stands for 'representational state transfer'. It determines what the API will look like. It is a set a rules that the developers follow when they create their API.
One of the rules state that you should be able to get a piece of data (i.e. resource) when you link to a specific URL.
Each URL is called a request while the data sent back is called a response.
The request is made up of four things:

The Endpoint

The endpoint is the URL you request for. Which is then made up of the following components:
The ROOT ENDPOINT which is the starting point of the API you are requesting from.
The PATH determines the resource you are requesting for. For each API you have to review the documentation to see what paths are available.
The QUERY PARAMETERS give you the option to modify requests with key-value pairs. They always begin with a ? and are then seperated with an &.

The Method

The method is the type of request you send to the server. There are five different types:
GET - Used to get a resource from a server. When a GET request is used the server looks for the data requested and send it back to you.
The GET request performs a READ operation.
POST - Used to create a new resource on a server
Performs a CREATE operation.
PUT and PATCH - Used to update a resource on a server.
Performs an UPDATE operation.
DELETE - Used to delete a resource from a server.
Perfroms a DELETE request.

The Headers

Headers provide information to both the client and the server. HTTP headers are property value pairs that are seperated by a colon.

The Data

The data is sometimes referred to as the 'body' or 'message', it contains information you want to be sent to the server. It is only used with the POST, PUT, PATCH and DELETE operations

cURL

APIs are usually written in reference to cURL, which is an command line utitily.
To make sure you have cURL downloaded, open the terminal and input:
curl --version
Once downloaded (if not already), input curl followed by the endpoint url you are requesting:
curl 'url of endpoint'
It will return all the HTML code for the particular URL, which we can then work with to receive what we need.

HTTP status codes

HTTP response status codes indicate whether a specific HTTP request has been successfully completed. In general, they follow the following rules:
200+ - request has succeeded
300+ - request is redirected to another URL.
400+ - An error that originates from the client has occurred.
500+ - An error that originates from the server has occurred.

HTTP response status must equal 200 for it to work.

XMLHttp ready states

When working with API, you will see the XMLHttpRequest.readyState();. This returns the state that the client is currently in. The XHR client exists in one of the following states:
0 - unsent, client has been created. open() not called yet.
1- opened, open() has been called.
2 - headers received, send() has been called. Headers and status are available.
3 - loading, downloading. repsonseText() holds partial data.
4 - Done, operation is complete.

For API to work, the ready state must equal 4.

With APIs, it is just a case of doing them again and again until they begin to make sense. All APIs will have documentation and usually a tutorial of how to use them.
Within this website, I have used emailJS API for the suggestions page.