Skip to content

[routes-b] POST /api/routes-b/webhooks/[id]/ping — send a test ping to a webhook #478

@davedumto

Description

@davedumto

Overview

Implement a `POST` handler that sends a test ping payload to a registered webhook URL so the user can verify their endpoint is working.

⚠️ Scope: Write your code only inside `app/api/routes-b/webhooks/[id]/ping/route.ts`. Do not touch any file outside this folder.


Create the route file

File: `app/api/routes-b/webhooks/[id]/ping/route.ts`

Handler logic

  1. Verify auth
  2. Find `UserWebhook` by `params.id` — verify ownership
  3. Build a test payload:

```typescript
const payload = {
event: 'ping',
timestamp: new Date().toISOString(),
webhookId: params.id,
}
```

  1. Sign it using HMAC-SHA256 with the webhook's `signingSecret`:

```typescript
import crypto from 'crypto'

const signature = crypto
.createHmac('sha256', webhook.signingSecret)
.update(JSON.stringify(payload))
.digest('hex')
```

  1. `POST` the payload to `webhook.targetUrl` with headers:

    • `Content-Type: application/json`
    • `X-LancePay-Signature: sha256=${signature}`
  2. Record success/failure and return the result

Expected response

```json
{
"success": true,
"statusCode": 200,
"targetUrl": "https://myapp.com/webhooks/lancepay"
}
```

If the ping fails (non-2xx or network error):
```json
{ "success": false, "statusCode": 500, "targetUrl": "https://myapp.com/webhooks/lancepay" }
```

Do not throw an error if the target URL returns a non-2xx — just record `success: false`.


Acceptance criteria

  • Returns `200` with `{ success, statusCode, targetUrl }`
  • Payload is HMAC-signed and sent with the signature header
  • Non-2xx from target returns `{ success: false }` — not a server error
  • Network failure is caught and returns `{ success: false, statusCode: 0 }`
  • Returns `403` if webhook belongs to another user
  • Returns `404` if webhook does not exist
  • Returns `401` for unauthenticated requests

Metadata

Metadata

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions