---
title: "Webhooks"
description: "Handle and register Shopify webhooks with HMAC verification."
canonical_url: "https://shopify.yan.ad/guides/webhooks"
---
# Webhooks

> Handle and register Shopify webhooks with HMAC verification.

## Handling Webhooks

Use `useShopifyWebhook()` to validate and process incoming webhooks. HMAC verification is handled automatically:

```ts [server/api/webhooks.ts]
export default defineEventHandler(async (event) => {
  const { topic, shop, payload } = await useShopifyWebhook(event)

  switch (topic) {
    case 'APP_UNINSTALLED':
      // Clean up shop data
      break
    case 'PRODUCTS_CREATE':
      // Handle new product
      break
  }

  return { success: true }
})
```

### `WebhookContext` return type

<table>
<thead>
  <tr>
    <th>
      Property
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Description
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        topic
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      The webhook topic (e.g., <code>
        APP_UNINSTALLED
      </code>
      
      )
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        shop
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      The shop domain
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        session
      </code>
    </td>
    
    <td>
      <code>
        Session | undefined
      </code>
    </td>
    
    <td>
      The offline session for the shop (if available)
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        payload
      </code>
    </td>
    
    <td>
      <code>
        Record<string, any>
      </code>
    </td>
    
    <td>
      The parsed webhook payload
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        apiVersion
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      The API version of the webhook
    </td>
  </tr>
</tbody>
</table>

## Registering Webhooks

There are two approaches to registering webhooks:

### Via `shopify.app.toml` (recommended)

For most apps, define your webhooks in `shopify.app.toml`. Shopify handles registration automatically via [managed install](https://shopify.dev/docs/apps/build/webhooks/subscribe#subscribe-to-webhooks-using-a-toml-file):

```toml [shopify.app.toml]
[webhooks]

  [[webhooks.subscriptions]]
  topics = ["app/uninstalled"]
  uri = "/api/webhooks"

  [[webhooks.subscriptions]]
  topics = ["products/create", "products/update"]
  uri = "/api/webhooks"
```

### Programmatically (per-shop)

Use `registerShopifyWebhooks()` when you need dynamic, per-shop webhook registration:

```ts [server/plugins/shopify.ts]
import { configureShopify, registerShopifyWebhooks } from '#shopify/server'

export default defineNitroPlugin(() => {
  configureShopify({
    hooks: {
      afterAuth: async ({ session }) => {
        await registerShopifyWebhooks(session)
      }
    }
  })
})
```

<tip>

For most apps, registering webhooks via `shopify.app.toml` is sufficient. Use `registerShopifyWebhooks()` only when you need dynamic, per-shop webhook registration.

</tip>

## Webhook Security

`useShopifyWebhook()` automatically handles HMAC verification using your `apiSecretKey`. If the HMAC is invalid, the request is rejected before your handler code runs.

<warning>

Do not use any middleware that consumes the raw request body before `useShopifyWebhook()` — the raw body is needed for HMAC verification.

</warning>


## Sitemap

See the full [sitemap](https://shopify.yan.ad/sitemap.md) for all pages.
