RunKit Endpoint Build APIs without any servers
Note: This is a beta release! We may make changes as Endpoint develops.
Endpoint is a fast and simple way to write code with Node.js that can respond to HTTP requests from anywhere. There's no need to manage servers or deployments, instead just write code in RunKit. Getting started is as easy as implementing one function. Here's a simple example:
The code above is now a real running endpoint. Changes will be reflected in real time. And just like with any notebook, you can (and should!) publish permanent versions as you develop your APIs. Go ahead, try curling it:
The Details
Any RunKit document that exports a function called endpoint
can serve requests. The function should have the same signature as the callback passed to server.listen
in node directly. The only change here is removing the explicit server creation and port binding.
exports.endpoint = function(request, response) {
// your code goes here
}
-
request
An instance of node's http.IncomingMessage, with full access to its properties and methods.
-
response
An instance of node's http.ServerResponse, also with full access to its properties and methods.
URLs
Every RunKit Notebook has a main url like:
https://runkit.com/runkit/endpoint-demo-1
as well as specific revision URLs like:
https://runkit.com/runkit/endpoint-demo-1/branches/master https://runkit.com/runkit/endpoint-demo-1/1.0.0
Notebooks access through endpoint are hosted on runkit.io
and each user has their own subdomain. Unlike the main notebook page, which has a shorthand for accessing the latest revision, you'll just need to specifically reference /branches/master
. Being explicit has an advantage; it means that not only will your notebook respond to urls like:
https://runkit.runkit.io/endpoint-demo-1/branches/master
It also responds to any sub-paths like these:
https://runkit.runkit.io/endpoint-demo-1/branches/master/hello/world https://runkit.runkit.io/endpoint-demo-1/branches/master/use/the/force
You can use the environment variable process.env.RUNKIT_MOUNT_PATH
to programatically check the base path of your endpoint, and process.env.RUNKIT_ENDPOINT_URL
to get the base URL of your endpoint.
Examples
Simple JSON API
Want to build a simple API that sends/receives JSON messages, without dealing with HTTP at all? We built a simple helper for this that sets it up for you. You can see how the helper is implemented, or check out this usage example. You can also return a promise directly, or use a done callback.
Using Endpoint with Express
We've built a simple wrapper to set up a fully compatible Express app with Endpoint. Just require our wrapper instead of express, and pass in the exports object to your app constructor. You can see how the helper is implemented, or check out this usage example.
Restrictions
Endpoint is only available over https
.
Although RunKit lets you use a top level await
at any time, you can't use a top level await before exporting your endpoint function.
Requests must terminate within 60 seconds. Endpoints are rate limited, and during the beta period they are restricted to non-commercial use. If you need to raise rate limits, or would like to use Endpoint commercially, just get in touch.