The Endpoint Function

Napkin includes its own Node.js 14.x runtime with additional features for handling HTTP traffic. To get access to incoming request data and outgoing response data, simply export a default function with 2 parameters like this:

export default (req, res) => {
  // any logic here

The function should have 2 parameters: req and res . For instance, you can access the incoming request parameters via the req.params function and send back content in the response via res.send. These objects are very similar to the Express' request and response APIs:

Incoming Request Data


type: Object

Contains key-value pairs of data submitted in the request body if the incoming Content-Type was application/json


type: Object

Contents of all cookies transmitted in the request

type: String

Contains the incoming request data as a string in case it came it came with a non-application/json Content-Type header.


type: Object

Contains key-value pairs of incoming form data if the incoming Content-Type request header is multipart/form-data


type: Function

Return the value for the the HTTP request header field field (case-insensitive match).


type: Object

Contains the incoming request headers as key-value pairs


type: String

The HTTP method of the incoming request


type: Function

Returns the value for the given path parameter, or undefined if no such parameter exists in the request


type: Object

Contains key-value pairs of the path parameters of the incoming request


type: String

The path part of the request URL


type: Object

Contains key-value pairs for any query parameters present in the incoming request route

Response Object


type: Function

Appends the specified value to the HTTP response header field. If the header is not already set, it creates the header with the specified value. The value parameter can be a string or an array


type: Object

Object for storing key-value pairs of response headers


type: Function

Stores the given Object or Array as JSON payload in the response body.


type: Function

Sends the given String back in the HTTP response body

res.set(field, [, value])

type: Function

Sets the response's HTTP header field to value


type: Function

Sets the response's status code to statusCode

Installing Modules

Once you've created an account, the Napkin editor allows you to install any NPM module or package.

Currently, all installed NPM modules are shared across a user's functions. Just install a module once and it will be there ready to use in any of your other functions. Currently, combined module storage space is limited to 250MB.

Napkin supports both CommonJS and ES6 module syntax, so both of the following work:

import _ from 'lodash'
// or
const _ = require('lodash')

Environment Variables

As a signed in user, you can add environment variables to your user functions:

  1. Go to Other tab > Environment Variables
  2. Add key and value

Using Environment Variables

Then, to use environment variables in Node.js, use the built-in process.env global property. For example, you can print all environment variables in your function:



You can upload files to use in your Napkin functions. Go to the Other tab in the Napkin editor and scroll down to Files. You can upload any file as long as its size is less than 1MB.

Once uploaded, your function can access your files via the /opt/files directory. You can access your files using the usual builtin modules for both Javascript and Python. For example, assuming you've uploaded a file called file.txt, here's how you can print the contents of the file:

import fs from 'fs'

const data = fs.readFileSync('/opt/files/file.txt', 'utf-8')

Key Value Store

The store module lets allows you to persist any valid JSON data type across your function runs:

  • a string
  • a number
  • an object (JSON object)
  • an array
  • a boolean
  • null
import { store } from 'napkin'

async store.get(key)

Returns: Object ({ data: ... })

Retrieve the value associated with the key key . The value is return in a dictionary with a single key, data

await store.put('myKey', 42)
const { data } = await store.get('myKey')
// 42

async store.has(key)

Returns: Boolean

Return whether or not the key-value store contains a key-value pair with key key

await store.put('myKey', 42)
let exists = await store.has('myKey'))
// true

await store.delete('myKey')
exists = await store.has('myKey')
// false

async store.put(key, value)

Returns: null

Save the given key-value pair in the Store. If the key-value pair already exists in the store, the value is updated accordingly.

await store.put('myKey', 42)
let exists = await store.has('myKey'))
// true

async store.size()

Returns: Number

Return the current size in bytes of the key-value store. Maximum size is 400 KB.

const size = await store.size()

// 128

async store.delete(key)

Delete the value stored under the key key

await store.put('myKey', 42)
let exists = await store.has('myKey')
// true

await store.delete('myKey')
exists = await store.has('myKey'))
// false

const { data } = await store.get('myKey')
// null