# APIs (Application Programming Interface)
---
APIs exist to make developers' lives easier. They provide a way for interaction and information exchange between a user or client and an application. Simply said, APIs are a way for bringing solutions to problems by abstracting lower-lever logic away from the developer.
---
Think of an API as a service where requests are handled without having to worry about how solutions are implemented. For example, an API can be a microservice which simply GETs a username and returns a greeting according to the time of the day.
---
APIs can be local or remote. Remote APIs require a server which listens for requests and serves appropriate response back to the requester. Those are hosted on servers and accessible via the web (http/https).
---
Tools to interact with Web-based APIs, such as cURL and Postman are popular among developers who want to test their queries before adding them to their projects.
---
To understand how an API works you should refer to the documentation and example usage given by the people who developed and maintain the specific API. Costs, usage, caching requirements, authorization and other important points on usage should be described as well if relevant.
---
A lot of APIs are paid and will offer different tiers depending on demand and usage from clients, for example measured in requests per second, etc.
Some APIs are easily accessible, whereas others require a secret key used for authorization and have restrictions on usage for billing or security purposes.
There are many open source APIs which you can use for your projects, such as: PokeAPI, Postcodes.io, data.police.uk, TFL API, GitHub (REST and GraphQL ones) and other.
---
A good API should be built to meet the demands of its users. Speed, security, reliability, accuracy and good documentation, simplicity, consistency and ease of usability are just a number of vital points to look out for before incorporating it into your project. Stay away from APIs which are slow, unreliable, depracated or abandoned (I'm looking at you, Zoopla :eyes:), or simply not well documented. At the end of the day, an API should make your life easier and not harder.
---
In the context of Web Development there are two popular ways of serving data, REST vs GraphQL. Certain APIs can handle multiple requests asynchronously which is felt in speed, whereas others are only limited to handling requests in a synchronous way and tend to be a lot slower to the point of being unusable. Node.js can handle requests asynchronously (concurrently), whereas Python (Django) does things in a sunchronous way, meaning one request must resolve first before resolving another one.
---
Representational state transfer (REST) vs GraphQL

---
Representational state transfer (REST) is a software architectural style that defines a set of constraints to be used for creating Web services. - Source: Wiki
---
Apollo Client is a comprehensive state management library for JavaScript that enables you to manage both local and remote data with GraphQL. Use it to fetch, cache, and modify application data, all while automatically updating your UI. Source: https://www.apollographql.com/docs/react/
---
Example of a REST GET request using fetch()
```javascript=
fetch('https://pokeapi.co/api/v2/pokemon/25')
.then(res => res.json()
.then(json => console.log(json)));
```
---
GitHub GraphQL API examples:
https://developer.github.com/v4/explorer/
```javascript=
query {
viewer {
login
}
}
```
```javascript=
query($number_of_repos:Int!) {
viewer {
name
repositories(last: $number_of_repos) {
nodes {
name
}
}
}
}
```
{"metaMigratedAt":"2023-06-14T22:50:58.688Z","metaMigratedFrom":"Content","title":"APIs (Application Programming Interface)","breaks":true,"contributors":"[{\"id\":\"4a393201-e032-4634-aabf-fb4f094fb8d4\",\"add\":3992,\"del\":231}]"}