How to integrate OpenPix with Slack
Published: 2022-09-26 11:57:52


# Why?
When working with instant payments, you want everything to be instant as well. So I'd like to receive a notification for every new transaction I got in my OpenPix account in real-time as well. To make this possible, I've created a small lambda that receives OpenPix Webhook payload and transforms it to Incoming Webhooks format and sends it to Slack. To implement this we need less than 50 lines of code, and I'm gonna show you.

### OpenPix
OpenPix is an instant payment startup from Brazil that allows companies to receive transactions using Pix. An instant payment method that is very effective. 
 Here's their website if you want to know more about it.

### Webhooks
Before going to our solution, let's understand what's a Webhook. The first is a service that allows one program to send data to another as soon as an event happens. It's the reverse of an API, where you have to make a request, webhooks send the data to you. For our integration, we'll use webhooks of OpenPix and Incoming Webhooks, a feature from Slack.
Incoming webhook allows external sources to share their content with your application, or in this case, Slack. That's what we need since OpenPix already provides you with a webhook payload like this one:

```json
{  
	"pixQrCode": null,  
	"charge": null,  
	"pix": {    
		"pixQrCode": null,    
		"charge": null,    
		"time": "2021-03-12T12:44:09.269Z",    
		"value": 1,    
		"transactionID": "ea83401ed4834b3ea6f1f283b389af29",   
		"infoPagador": "OpenPix testing",    
		"customer": {      
			"correlationID": "9134e286-6f71-427a-bf00-241681624586", 
			"email": "email1@example.com",      
			"name": "Loma",      
			"phone": "+5511999999999",      
			"taxID": {        
				"taxID": "47043622050",        
				"type": "BR:CPF"      
			}    
		},    
		"payer": {      
			"correlationID": "9134e286-6f71-427a-bf00-241681624586",
			"email": "email1@example.com",      
			"name": "Loma",      
			"phone": "+5511999999999",      
			"taxID": {        
				"taxID": "47043622050",        
				"type": "BR:CPF"      
			}    
		}  
	},  
	"company": {    
		"id": "624f46f9e93f9f521c8308d7",    
		"name": "Pizzaria do José",    
		"taxID": "4722767300014"  },  
		"account": {    
			"clientId": "ZOJ64B9B-ZM1W-89MI-4UCI-OP2LVIU6NY75"  
		}
	}
```

### How
To start with, you'll need to create a new app on your slack workspace and allow it to receive incoming webhooks. Check out here how to do it: [Creating an App](https://api.slack.com/messaging/webhooks)

After that create a lambda function, the function will receive the payload from OpenPix and format it to send to Slack. You could  do it without the lambda, but I used it to scale(if you receive a lot of transactions, consider using it as well). Lambda integrates easily with API Gateway because you can trigger the function just by sending a request to the API. As I told you, the function will transform the payload from OpenPix to send it to our workspace, so you can take the payload above as an example. 
Creating a new webhook on OpenPix is easy, just hit API/Plugins and then New Webhook, you'll see this page.

![OpenPix Dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vtuzh8teg8zrlx7aaf74.jpg)

Put your API Gateway URL in it and select when you want the webhook to be triggered. That's all you'll need to do on the platform.

Here's an example of what the notification looks like on Slack, using a basic implementation. You can easily make it as you wish by following the docs [here](https://api.slack.com/messaging/webhooks#advanced_message_formatting).

![Slack Example](https://raw.githubusercontent.com/LeAlencar/openpix-slack/main/.github/imgs/example.png)

Congrats, you just integrated OpenPix with Slack. You can check out the code I made for further instructions on how to implement it on my [repo](https://github.com/lealencar/openpix-slack). If you need, feel free to reach [out](https://twitter.com/lealencar__).