Skip to content

Latest commit

 

History

History
74 lines (52 loc) · 2.8 KB

README.md

File metadata and controls

74 lines (52 loc) · 2.8 KB

Simple webhook forwarder script to send webhooks from a hosted url to your local machine without hassle of setting up tunnels, or port forwarding your own machine, uses webhooks to communicate between client and server

Why?

As opposed to using something like ngrok, this lets you use your own domain/server straight out of the box, which also means you can use the same url to test your webhooks locally while developing.

Also integrates directly into your express app and calls your post endpoints based on the webhook url that is hit, so you don't need to make any changes to your existing configuration

Installation

npm i --save-dev webhook-simple-forwarder

Client Setup (Where you want to receive webhooks)

// index.js
const webhook_forwarder = require('webhook-simple-forwarder')
const express = require('express')
const app = express()

app.use(express.urlencoded({ extended: false }))
app.use(express.json())

app.post('/wh/some-webhook', function(req, res) {
    console.log("Received Webhook!!")
    console.log(req.body)
    
    res.status(200).send("ok")
})

const server = app.listen(process.env.PORT, () => { 
    if (process.env.NODE_ENV === 'Development')
        webhook_forwarder.client(
            server, 
            'forward.exampledomain.com:3000', 
            'secret-key', // replace 'secret-key' with your own
            '/base_path', // optional, can be any base path, for example if you want to allow multiple different client to use the same server
            true // true to omit base url from final request url.
        )
})

Server Setup (Public endpoint for webhooks to come in)

Place this on a server, that can be accessed publicly (e.g. on somedomain.com or via ip), point your webhook to that server, and once you run the client your webhook endpoints will be hit

// index.js
const webhook_forwarder = require('webhook-simple-forwarder')
webhook_forwarder.server('secret-key', /*optional port, default: 3000 */) // replace 'secret-key' to match client

Additional Data Returned

req.body._url - provides original url webhook was sent to req.body._headers - provides original headers sent with the webhook req.body - original webhook body

What endpoints can I use?

Use the same endpoints that you have already.

If you have a post endpoint called "wh/some-webhook" and your webhook forwarder server is located at forward.exampledomain.com your webhook url will be forward.exampledomain.com:3000/wh/some-webhook

This library supports unlimited endpoints.

How to setup my server?

Any server with a public ip or domain will do, you could probably even use cloud functions for this

Contributing & Issues

This package was made to fix a specific problem I had, therefore some features may be missing.

Feel free to open an issue or make a pull request.