Zuplo

Custom Modules

Use npm packages and third-party libraries in your Zuplo handlers and policies.

Include Custom Modules

This sample demonstrates how to bundle custom node modules to use in your Zuplo project. In order to use this in your own Zuplo project, you must connect to source control and clone your project locally.

1/ Install Your Custom Module

First, install your custom module into your project:

Terminalbash
npm install YOUR_MODULE

2/ Bundle the Module

Bundle the module using tsdown. You can use other bundling tools as well, but tsdown is simple and effective for this purpose.

Terminalbash
npx tsdown ./node_modules/YOUR_MODULE --format esm --platform browser --out-dir ./modules/third-party/YOUR_MODULE

For convenience, add this as a script in your package.json:

JSONjson
{
  "scripts": {
    "bundle": "npx tsdown ./node_modules/YOUR_MODULE --format esm --platform browser --out-dir ./modules/third-party/YOUR_MODULE"
  }
}

Then run:

Terminalbash
npm run bundle

3/ Use the Module

Inside of your code you can now import the custom module through the path. One thing to note is that you will not have code completion for this module. See the next step for information on how to include type definitions.

TypeScriptts
import MyModule from "./third-party/my-module";

4/ Optional: Type Definitions

By default this script will not copy or generate any type definition files. This means your custom module will be used as any any object and you wont have code completion.

If you want to bundle the type definitions (*.d.ts) files, we recommend copying them manually to the output directory - i.e. ./modules/third-party/YOUR_MODULE/index.d.ts

Updating the Bundle

If you would like to update your custom module, you will need to first install the new module version. You can install the latest version of a module by running npm install YOUR_MODULE@latest, then re-run the npm run bundle command. This will overwrite the existing bundle with the new version of the module.

Use this example locally

To develop with this example locally, you can create a new Zuplo project using our CLI

Terminalbash
npx create-zuplo-api@latest my-api --example custom-module

Then, in the project directory run the following commands:

Terminalbash
npm install
npm run bundle
npm run dev

Related Examples

Explore more examples in this category

Circuit Breaker

Programmability

Protect your backend from cascading failures by automatically blocking traffic when error rates exceed a threshold.

Custom Rate Limiting

Programmability

Customize the error response users see when they hit your API rate limits.

Dynamic Rate Limits

Programmability

Set different rate limits based on user subscription tier or API key metadata.

Idempotency Keys

Programmability

Prevent duplicate API requests and ensure safe retries for payments and critical operations.