Usage Patterns

Notaify offers multiple ways to capture errors depending on your preference and framework. Choose the one that fits your workflow best.

1. Global Auto-Capture

The simplest and most powerful approach. After calling notaify.init(), all unhandled errors and unhandled promise rejections in your app are automatically captured and reported — no try-catch needed anywhere.

javascript
// index.js / server.js import notaify from '@notaify/node' notaify.init({ apiKeyId: process.env.NOTAIFY_API_KEY_ID, apiKey: process.env.NOTAIFY_API_KEY, }) // That's it. Every unhandled error from this point is auto-reported.
Best for: Any Node.js app where you want zero-effort, blanket error coverage.

2. Manual Capture

For fine-grained control. Use this inside a try-catch when you want to handle the error yourself but also report it to Notaify.

javascript
import notaify from '@notaify/node' try { await riskyOperation() } catch (error) { // Report to notaify await notaify.capture(error) // optionally re-throw or handle gracefully }

You can also pass additional context to be included in your reports:

javascript
await notaify.capture(error, { userId: '123', route: '/api/payment', severity: 'critical' })
Best for: Specific critical operations where you want targeted error reporting with extra context.

3. Async Handler Wrapper

Wraps any async route handler. If an error is thrown inside, it is automatically captured and reported. Clean, no try-catch boilerplate.

javascript
import { notaifyHandler } from '@notaify/node' // Express app.get('/users', notaifyHandler(async (req, res) => { const users = await db.getUsers() res.json(users) })) // Next.js API Route export const GET = notaifyHandler(async (req) => { const data = await fetchData() return Response.json(data) })
Best for: Next.js API routes, individual Express routes, or any async function where you want per-route error capture without boilerplate.

4. Express / Fastify Error Middleware

Plug Notaify into your Express or Fastify app as error-handling middleware. Any error passed to next(error) in Express, or thrown in a Fastify route, is automatically captured.

express
import express from 'express' import { notaifyMiddleware } from '@notaify/node' const app = express() // your routes app.get('/route', async (req, res, next) => { try { await doSomething() } catch (err) { next(err) // passes to middleware } }) // Add AFTER all routes app.use(notaifyMiddleware())
fastify
import Fastify from 'fastify' import { notaifyMiddleware } from '@notaify/node' const fastify = Fastify() fastify.setErrorHandler( notaifyMiddleware({ framework: 'fastify' }) ) // route definitions...
Best for: Express and Fastify apps where you want centralized error handling in one place.