---
title: "How to use webhooks on Optimism"
description: "What are Optimism Webhooks and How to Use Them in Web3"
---

# How to use webhooks on Optimism

If you want your web3 application to communicate with third-party apps or services, you’re going to love webhooks. In this tutorial, we’ll learn how to set up and use webhooks on [Optimism](https://www.alchemy.com/layer2/optimism/?a=a40402f9aa). Then we’ll show you how you can set up automated notifications for sending and receiving ETH on Optimism in Slack.

## What is a webhook?

[**Webhooks**](https://www.alchemy.com/overviews/what-is-a-webhook) are tools web3 developers can use to allow third-party apps or services to communicate with applications when an event occurs.  This technology is more efficient than continuously polling nodes to check if a particular state has changed via an API, as webhooks allow these servers to push notifications to the app at the time of the event.

[Alchemy webhooks](https://www.alchemy.com/docs/reference/notify-api-quickstart) have a lot of potential to improve the user experience and drive the engagement of web3 applications and services, such as tracking NFT sales in a Discord channel or sending wallet activity to a Slack channel.

## What Optimism events can be tracked with webhooks?

**There are three types of Optimism events we can receive notifications for via Alchemy webhooks:**

1. Mined transactions - a transaction is completed
1. Dropped transactions - a transaction fails or is dropped from the mempool
1. Address Activity - tokens are received or transferred

For this tutorial, we will be tracking _Address Activity_ events, where we’ll receive Slack notifications for sending and receiving ETH on Optimism. 

## How to set up Optimism webhooks

Here is the 10-step process for setting up Optimism webhooks and automating notifications to Slack. This process can be followed for many other 3rd-party platforms supported by Zapier like Discord, Twitter, and more!

### 1. Create an Ethereum wallet

We need an Ethereum wallet to send and receive transactions. For this tutorial, we’ll use **[MetaMask](https://www.alchemy.com/dapps/metamask)**, a browser-based digital asset wallet to manage Ethereum accounts. [Download MetaMask](https://metamask.io/download.html) and create a free account

### 2. Sign into Alchemy

To make things easy, [sign up for a free Alchemy developer account](https://dashboard.alchemy.com/signup), so you can communicate with the Ethereum chain without having to run your own nodes.

### 3. Create an API key

Next, create a new app to generate an API key and to [connect to the Optimism test network](https://www.alchemy.com/docs). 

Navigate to the _Create App_ page in your Alchemy Dashboard by hovering over _Apps_ in the navbar and click _Create App._

<ImageBlock
  src="https://media.alchemy.com/1704209670-create-api-key-alchemy.jpg"
  alt="Alchemy dashboard: creating a new app to generate an Optimism API key"
  width={1600}
  height={912}
/>

Name your app \(we chose “optimism_webhooks”\), and write a short description. 

Next, select _Optimism_ for your chain and choose _Optimism Sepolia_ for your network.

Then, click _Create app._

Now, click on the newly created app link under _Apps_ and click _Add to Wallet_.

You should now be connected to the Optimism Sepolia testnet on Metamask!

### 4. Get testnet ETH from a Sepolia faucet

To deploy your smart contract to the Optimism test network, [get ETH from Alchemy's Sepolia faucet](https://www.alchemy.com/faucets/ethereum-sepolia).

First, change your MetaMask network to Ethereum Sepolia. On the faucet page, paste your wallet address and request test ETH.

Then, you should see test ETH in your MetaMask account on Sepolia.

### 5. Get OP Sepolia ETH

The fastest path is Alchemy's [Optimism Sepolia faucet](https://www.alchemy.com/faucets/optimism-sepolia). You can also bridge Sepolia ETH to OP Sepolia with the [Superchain Bridges UI](https://app.optimism.io/bridge).

Once you have OP Sepolia ETH in your wallet, continue to the webhook setup.

### 6. Create a zap and an Optimism webhook URL

Now, [create your Zapier account](https://zapier.com/sign-up) or log into your account if you already have a Zapier account.

Next, in the dashboard, click _Create Zap_ in the top left corner.

We will now create the webhook URL for sending the webhook response from Alchemy to Zapier.

Under _Trigger_, search and select _Webhooks by Zapier_ as the app event/trigger.

For the _Trigger Event_, select _Catch Hook_ since Zapier is waiting for Alchemy’s address activity webhook to send the event data to the URL whenever a transfer occurs.

Click _Continue_ to the _Set up trigger_ section and copy the _Custom Webhook URL_.

<ImageBlock
  src="https://media.alchemy.com/1704209746-zap-and-optimism-webhook-url.png"
  alt="Zapier Zap editor: copying the custom webhook URL from the Catch Hook trigger"
  width={1600}
  height={634}
/>

### 7A. Create an Optimism webhook

Now, we need to create the address activity webhook to track transfer events and send that data off to the Zapier webhook URL.

We will show two ways that you can set up the webhook, first from Alchemy’s **Notify tab** within the dashboard, and second programmatically by using **Alchemy’s Notify API** \(recommended for dealing with address activity webhooks for 10\+ addresses\).

1. Make sure you are in the _Ethereum \+ L2_ ecosystem on the upper left of the Alchemy dashboard
1. Navigate to the Notify tab and click _\+ Create Webhook_ button in the *Address Activity* section
1. Select _Chain_ to be _Optimism_
1. Select _Network_ to be *Sepolia*
1. Paste in the webhook URL you copied from Zapier in the last step
1. Paste your MetaMask wallet address into the _Ethereum Addresses_ section.

NOTE: If you want to test if the Alchemy webhook successfully sends requests to Zapier’s webhook URL before confirming the creation, click on _Test Webhook_ next to the webhook URL. Or if you have already created the webhook but want to test again, click on the three dots by your webhook and select _Send Test Notification_.

Now, go back to Zapier and continue to the _Test trigger_ section, and click _Test trigger_.

If you see a “We found a request message,” congratulations!

### 7B. Create an Optimism webhook using notify API

If you want to create webhooks for multiple Optimism addresses, usw the[ Notify API](https://www.alchemy.com/docs/reference/webhooks-overview). To get started, copy your Alchemy authentication token \(X-Alchemy-Token\) from the upper right corner of your Notify dashboard under the _AUTH TOKEN_ button.

<ImageBlock
  src="https://media.alchemy.com/1704209787-alchemy-auth-token.png"
  alt="Alchemy dashboard showing where to copy the Auth Token for the Notify API."
  width={1600}
  height={152}
/>

Then, navigate to your command line and type the following:

`*curl https://dashboard.alchemy.com/api/create-webhook \`

`*-X POST \`

`*-H "X-Alchemy-Token":"your-X-Alchemy-Token" \`

`-d '\{"network":"OPT_SEPOLIA","webhook_type":"ADDRESS_ACTIVITY","webhook_url":"your-Zapier-Webhook-URL", "addresses":["your-Ethereum-Address"]\}'``
`

Remember to use your own _your-X-Alchemy-Token_, _your-Zapier-Webhook-URL,_ and _your-Ethereum-Address_ \(aka, the wallet address you pasted\), and you can add as many Ethereum addresses as you’d like to track their transfer events.

After refreshing, you should see the newly created address activity webhook appear in your Notify dashboard with all your specified parameters.

### 8. Parse Optimism webhook responses in zapier

Given the webhook response from Alchemy, we need to parse it in Zapier to create an action that sends an alert whenever our Optimism address successfully sends a transaction.

Under the _Action_ section, select the _Filter_ option on the right-hand side.

In _Filter setup & testing_, we want to continue if the _Event Activity From Address_ \(or you can simply search for _fromAddress_\) _\(Text\) Exactly matches_ our Optimism address \(paste it into the last text field\).

If you click _Continue_ and find that “Your Zap would not have continued,” don’t worry! The test notification was not sent from the wallet so this message is expected.

<ImageBlock
  src="https://media.alchemy.com/1704209834-optimism-webhook-responses-zapier.png"
  alt="Zapier filter setup: matching the webhook fromAddress to the Optimism wallet address"
  width={1600}
  height={912}
/>

### 9. Integrate your Optimism webhooks with slack

First, let’s create a Slack account and workspace. Create an account on [Slack](https://slack.com/) or log into your account if you already have one. Then, create a new workspace. We named ours _Optimism Wallet Alerts_, but feel free to use your own creativity!

#### Handle alerts for sending tokens

Next, we want to send a message if our wallet is the sender of a **transfer** **event**. 

1. Click _“_\+” at the bottom
1. Search for and select _Slack_ in the new _Action_ section
1. For the _Action Event_, choose _Send Channel Message_
1. After clicking _Continue_, sign in to your Slack account
1. Fill out the fields to send our message in the _Set up action_ section
1. Select the channel you want the alerts to be sent in \(_Optimism-Wallet-Alerts_ in our case\).

In the message text, you can get creative and choose what fields and values you want to send.

We chose these fields:

<ImageBlock
  src="https://media.alchemy.com/1704209860-alerts-for-sending-tokens.png"
  alt="Zapier Slack action: message fields chosen for Optimism token transfer alerts"
  width={864}
  height={133}
/>

You can leave everything else on the default option.

Click _continue_, then _test and continue_.

If no errors pop up, that means we’re good to go! A Slack message should have been made on your account. Now, publish the Zap. 

Congrats! Now, whenever your Optimism wallet sends out a transaction, our Zap will send a message detailing that transaction.

You can also view the run details of your Zap on the Zap History page in the Zapier dashboard. 

### 10. Send a message to handle receiving tokens on Optimism

We also want to send alerts when our wallet is on the receiving end of an Optimism transaction. 

First, edit the _continue only if_ action.

Click on _Filter Setup & Testing_ and press the “\+ or” button, filling in _Event Activity To Address_ \(or you can simply search for _fromAddress_\) and _\(Text\) Exactly matches_ our Optimism address \(paste it into the last text field\).

Sweet, now our Zap will run when the Wallet is also a recipient!

Next, let’s create a _Code by Zapier_ action between our _Only continue if…_ and _Send Channel Message in Slack_ actions.

You can choose either _Run Python_ or _Run Javascript_. We will choose the latter but the logic works the same for Python.

After clicking _Continue_, we will set up our input data under _Set up action_ as follows:

<ImageBlock
  src="https://media.alchemy.com/1704209961-run-javascript-code-zapier.png"
  alt="Code by Zapier action: setting up input data for the JavaScript step"
  width={1600}
  height={862}
/>

We are parsing through the webhook response again because our current notification is only written for when our wallet **sends** ETH. Therefore, we need to customize them for when our wallet **receives** ETH.

In the code block, paste in the following code \(read the comments to see what each line means\):

`// Since JavaScript is case-sensitive, we will be comparing addresses in upper case const address = "Wallet-Address".toUpperCase\(\);
// Define Account 1 \(Our wallet\)
let account\_1 = "";
// Define Account 2 \(the user our wallet is sending to / receiving from\)
let account\_2 = "";
// Define to use "to" or "from" in message
let t\_or\_f = "";
let f\_or\_t = "";
if \(inputData.fromAddress.toUpperCase\(\) == address\) \{
    // If Wallet is sending, then it is the fromAddress
    account\_1 = inputData.fromAddress;
    account\_2 = inputData.toAddress;
    t\_or\_f = "from";
    f\_or\_t = "to";
\} else if \(inputData.toAddress.toUpperCase\(\) == address\) \{
    // If Wallet is receiving, then it is the toAddress
    account\_1 = inputData.toAddress;
    account\_2 = inputData.fromAddress;
    t\_or\_f = "to";
    f\_or\_t = "from";
\}`
`//` Output the fields to be used in email
`output = \[\{account\_1: account\_1, account\_2: account\_2, t\_or\_f: t\_or\_f, f\_or\_t: f\_or\_t\}\];`

Again, remember to **replace Wallet-Address** with your MetaMask wallet address. When testing the action, don’t worry if it doesn’t assign anything to the output variables.

Lastly, go to the _Send Channel Message in Slack_ action and click on the _Set up action_ section.

We will now replace the generic fields from the webhook response in the message text with the more dynamic outputs from our Javascript code, which are customized depending on whether our Wallet sent or received tokens:

<ImageBlock
  src="https://media.alchemy.com/1704209901-send-channel-message.png"
  alt="Send Channel Message in Slack action and click on the Set up action section"
  width={876}
  height={173}
/>

Click _Continue_ and test your action. Then publish!

Congrats! You just finished creating Optimism webhooks!
