41 lines
2.5 KiB
Plaintext
41 lines
2.5 KiB
Plaintext
---
|
|
title: Hooks Overview
|
|
label: Overview
|
|
order: 10
|
|
desc: Hooks allow you to add your own logic to Payload, including integrating with third-party APIs, adding auto-generated data, or modifing Payload's base functionality.
|
|
keywords: hooks, overview, config, configuration, documentation, Content Management System, cms, headless, javascript, node, react, nextjs
|
|
---
|
|
|
|
Hooks allow you to execute your own logic during specific events of the Document lifecycle. With Hooks, you can transform Payload from a traditional CMS into a fully-fledged application framework. They allow you to perform business tasks, third-party integrations, etc. during precise moments within the data lifecycle.
|
|
|
|
There are many use cases for Hooks, including:
|
|
|
|
- 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
|
|
|
|
There are three main types of Hooks in Payload:
|
|
|
|
- [Collection Hooks](/docs/hooks/collections)
|
|
- [Global Hooks](/docs/hooks/globals)
|
|
- [Field Hooks](/docs/hooks/fields)
|
|
|
|
<Banner type="warning">
|
|
<strong>Reminder:</strong>
|
|
Payload also ships a set of _React_ hooks that you can use in your frontend application. Although they share a common name, these are very different things and should not be confused. [More details](../admin.hooks).
|
|
</Banner>
|
|
|
|
## Async vs. synchronous
|
|
|
|
All Hooks can be written as either synchronous or asynchronous functions. If the Hook should modify data before a document is updated or created, and it relies on asynchronous actions such as fetching data from a third party, it might make sense to define your Hook as an asynchronous function. This way you can be sure that your Hook completes before the operation's lifecycle continues. Async hooks are run in series - so if you have two async hooks defined, the second hook will wait for the first to complete before it starts.
|
|
|
|
If your Hook simply performs a side-effect, such as updating a CRM, it might be okay to define it synchronously, so the Payload operation does not have to wait for your hook to complete.
|
|
|
|
## Server-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.
|