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.
// 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.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.
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:
await notaify.capture(error, {
userId: '123',
route: '/api/payment',
severity: 'critical'
})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.
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)
})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.
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())import Fastify from 'fastify'
import { notaifyMiddleware } from '@notaify/node'
const fastify = Fastify()
fastify.setErrorHandler(
notaifyMiddleware({ framework: 'fastify' })
)
// route definitions...