Skip to main content

Overview

You can create Node.js/TypeScript APIs using Stormkit. During deployment, your API functions are automatically packaged and deployed to AWS Lambda with filesystem-based routing.
API Hello World example
Function timeouts are set at 15 seconds by default. If you require a different timeout, please inform us, and we can adjust it to suit your workflow.

How It Works

During build time, Stormkit:
  1. Checks if there is a .stormkit/api folder
  2. When found, uploads the folder to a Lambda function
  3. The entry file takes the Request and calls the relevant file based on filesystem routing
  4. Returns 404 if no matching route is found

Write and Deploy Your API

1

Create API directory

Create an /api folder in the top level of your repository
2

Create endpoint file

Create an /index.ts file in the /api folder
3

Export handler function

Each file must export a default function with the signature shown below
4

Deploy

Deploy your application. Stormkit will automatically build and deploy your API

Basic Example

api/index.ts
api/user/subscribe.ts

Filesystem Routing

The table below shows how the API routing works:
For more details on how the filesystem routing works, check the source code of the matchPath function.
Now go ahead and deploy your application. When Stormkit detects an /api source folder, it checks whether it is already built or not. If the /api folder is not yet built, Stormkit tries to build your API using Webpack and then deploys the output to the lambda function. This process is automatic.

Routing Features

Dynamic Routes

Use square brackets for dynamic segments:
api/users/[id]/index.ts

Matching by Request Method

By default, files are matched through all requests. If you want to restrict certain endpoints with a request method, you can specify the method in the file name, right before the extension:

GET Requests

index.get.ts - Only handles GET requests

POST Requests

index.post.ts - Only handles POST requests

PUT Requests

index.put.ts - Only handles PUT requests

DELETE Requests

index.delete.ts - Only handles DELETE requests

Ignore Certain Files

If a file name starts with an underscore (_), the file won’t be matched. If the directory starts with an underscore (_), the whole subdirectory tree will be ignored. This is useful to organize helper methods in different files.

Working with Request Data

Reading Request Body

api/users/create.post.ts

Query Parameters

api/search.ts

Custom Builds

If your source code contains more complex use cases and Stormkit fails to build, you can build the source code yourself. Here’s the webpack config Stormkit uses to build the API. You can copy this and extend it based on your needs.
webpack.config.ts

Testing Locally

In order to test the API locally, install the @stormkit/cli package.
1

Install CLI

2

Update package.json

package.json
3

Run development server

4

Access API

You can access the API from http://localhost:9090/api

API in Action

If you wish to see the API in action promptly, take a look at our template project, utilizing Vite.js as the build tool. This project encapsulates server-side rendering (SSR), API functionality, and a single-page application.

Best Practices

  • Use TypeScript - Type safety helps catch errors early
  • Validate inputs - Always validate request data
  • Handle errors - Use try-catch blocks and return appropriate error codes
  • Keep functions focused - Each endpoint should do one thing well
  • Use helper files - Organize shared code in _helpers.ts files
  • Set proper headers - Always set Content-Type and other relevant headers
  • Environment variables - Use env vars for configuration and secrets
  • Test locally - Use @stormkit/cli to test before deploying