- Modify data before it is read or updated
- Encrypt and decrypt sensitive data
- Integrate with a third-party CRM like HubSpot or Salesforce
- Send a copy of uploaded files to Amazon S3 or similar
- Process orders through a payment provider like Stripe
- Send emails when contact forms are submitted
- Track data ownership or changes over time
Root Hooks
Root Hooks are not associated with any specific Collection or Field. They are useful for application-wide side effects, such as when an error occurs at the application level. To add Root Hooks, use thehooks property in your FrogBot Config:
afterError
TheafterError Hook is triggered when an error occurs in the FrogBot application. This can be useful for logging errors to a third-party service, sending an email to the development team, logging the error to Sentry or DataDog, etc. The output can be used to transform the result object / status code.
afterError Hook:
The hook can return an object to change the error response, or nothing to keep it:
Awaited vs. non-blocking hooks
Hooks can either block the request until they finish or run without blocking it. What matters is whether your hook returns a Promise. Awaited (blocking): If your hook returns a Promise (for example, if it’s declared async), FrogBot will wait for it to resolve before continuing that lifecycle step. Use this when your hook needs to modify data or influence the response. Hooks that return Promises run in series at the same lifecycle stage. Non-blocking (sometimes called “fire-and-forget”): If your hook does not return a Promise (returns nothing), FrogBot will not wait for it to finish. This can be useful for side-effects that don’t affect the outcome of the operation, but keep in mind that any work started this way might continue after the request has already completed. Declaring a function with async does not make it “synchronous.” The async keyword simply makes the function return a Promise automatically — which is why FrogBot then awaits it. AwaitedServer-only Execution
Hooks are only triggered on the server and are automatically excluded from the client-side bundle. This means that you can safely use sensitive business logic in your Hooks without worrying about exposing it to the client.Custom Error Messages
You might want to return custom error messages in the API or admin panel in your hooks, if an operation cannot be performed - e.g. if a rate limit was exceeded. For this you can leverage theAPIError class like this:
Performance
Hooks are a powerful way to customize the behavior of your APIs, but some hooks are run very often and can add significant overhead to your requests if not optimized. When building hooks, combine together as many of these strategies as possible to ensure your hooks are as performant as they can be.Writing efficient hooks
Consider when hooks are run. One common pitfall is putting expensive logic in hooks that run very often. For example, theread operation runs on every read request, so avoid putting expensive logic in a beforeRead or afterRead hook.
beforeChange or afterChange hook, which only runs when a document is created or updated.