Hello, I am trying to create a webhook in monday that will commnicate with api gateway and lambda from aws but I am having troubles returning the challenge.
What language are you using?
Do you have some rough initial code you could share?
I can tell you without doubt that what you’re trying to do has been done before.
Are you using API Gateway HTTP API or REST API (I highly recommend HTTP API for monday because its faster, cheaper, and 99% of the time with monday all you are doing is a proxy to lambda). A lambda function URL is another alternative, if you don’t need a custom domain and don’t want to build out routes (for example a one off lambda for something).
Depending on if its REST API or HTTP API/Lambda Function URL the payload sent to the lambda is different.
First step is console.log(event.body)
to start seeing what is actually being sent. 99/100 times the struggle is that you’re not actually accessing something that exists yet - its a string, not object.
async function handler(event, context) {
if (typeof event.body !== "string") {
return {
statusCode: 400,
body: "missing body",
headers: {
"content-type": "text/plain"
}
}
}
const body = JSON.parse(event.body);
if (typeof body.challenge === "string") {
return {
statusCode: 200,
body: JSON.stringify({
challenge: body.challenge
}),
headers: {
"content-type": "application/json"
}
}
}
// now you can handle the actual webhooks here
}
Hello, I am using python3.9 and a REST API Gateway. I am just trying to return the challenge. Not sure it’s the good way:
import json
import urllib3
def lambda_handler(event, context):
print("Printing event -------------")
print(event)
print("Printed event -------------")
return {
'statusCode': 200,
'headers': {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": 'GET, POST, PUT, DELETE, OPTIONS'
},
'body': json.dumps(event)
}
The event object has a significant structure BESIDE the request body - the body is a property on the event, and is delivered as a JSON string.
You need to be looking at:
body = json.dumps(event.body)
that will be the body you need to return for the challenge. But you really need to look to see if body.challenge
is a string to see if the request even IS a challenge or is an actual webhook payload.
Thanks @codyfrisch for your contribution. Yes actually that was it, just returning the body property of the event and problem solved? That’s it looks like in nodeJs:
export const lambdaHandler = async (event, context) => {
console.log(event)
const body = event.body
console.log(body)
try {
return {
'statusCode': 200,
body
}
} catch (err) {
console.log(err);
return err;
fairly close, just remember this is your challenge response.
Regular payloads will ALSO be sent to this same lambda - so you have to determine if the payload is a challenge or a response when it comes in.
If this is a one time use - a single webhook instance on one board, you can pass the challenge once and remove the code. But you’ll need to readd it if you then add a new webhook or such that uses the same lambda. Or you JSON.parse(body) and then check if challenge exists and handle it, otherwise you continue to your regular code.
Right! I thought of that and I just passed the challenge once to set the url and removed the code for the next step
Thanks so much @codyfrisch . Really appreciate your help