Appearance
What is an API?
Local first API
In Pinokio, APIs are local JavaScript classes that behave like remote servers.
It's easy to understand when we compare it to traditional APIs.
Traditional API vs. Pinokio API
Traditional API
Here's an example "traditional" API request:
POST /test HTTP/1.1
Host: foo.example
Content-Type: application/x-www-form-urlencoded
Content-Length: 27
field1=value1&field2=value2
Basically it's making a:
- POST request
- to the route /test
- at a remote server foo.example
- by passing the key value pairs field1=value1&field2=value2
Pinokio API
With Pinokio, all APIs exist locally.
Instead of making a request to a remote server, the API requests go to locally installed JavaScript modules.
Here's an example Pinokio request:
json
{
"uri": "https://github.com/cocktailpeanut/sum.git/api.js",
"method": "sum",
"params": [1,2,3,4,5]
}
Here's how it works:
- First download the JavaScript module at
https://github.com/cocktailpeanut/sum.git
(NOTE: the URI is used to DOWNLOAD the entire repository, not to make a reuqest to it) - Resolve the endpoint by importing the JavaScript module at
api.js
- Look up the exported
sum()
inside theapi.js
file - Pass in the
params
to the resolvedsum()
method. - The
sum()
method does its job and returns a response.
Basically, everything happens locally but it resembles a network request.
Building a custom API
Therefore, building a custom API simply means writing a JavaScript module that follows a certain convention (will be explained in the following sections).
All you need to do is write a single JavaScript module while following the Pinokio JavaScript API convention, and you should be able to immediately start calling these API "backends" using JSON scripts.