Skip to content

Instantly share code, notes, and snippets.

@warrenkc
Created November 14, 2024 08:58
Show Gist options
  • Save warrenkc/df8fa88c06f2aa71838085cecbc75a74 to your computer and use it in GitHub Desktop.
Save warrenkc/df8fa88c06f2aa71838085cecbc75a74 to your computer and use it in GitHub Desktop.
Mailgun API Create Route
# Example from Mailgun: https://documentation.mailgun.com/docs/mailgun/api-reference/openapi-final/tag/Routes/#tag/Routes/operation/post-v3-routes
import requests
url = "https://api.mailgun.net/v3/routes"
data = {
"priority": "0",
"description": "it's a new route",
"expression": "match_recipient('.*@gmail.com')",
"action[0]": "forward("http://myhost.com/messages/")"
}
headers = {"Content-Type": "multipart/form-data"}
response = requests.post(url, data=data, headers=headers, auth=('<username>','<password>'))
data = response.json()
print(data)
# This example from Mailgun above will not work.
#Priority should be 0 or another int.
"priority": 0
# Expression is wrong. quotes are wack. It should be like this.
"expression": f'match_recipient("{recipient_address}")'
# Action is wrong. It should be a list.
"action": [f'forward("{forward_address}")'] # Action should be a list
}
# Headers is the wrong content type. It should be as below.
headers = {"Content-Type": "application/x-www-form-urlencoded"}
@warrenkc
Copy link
Author

I was working with the API from Mailgun. Their own documented examples don't work. So I explain here what to fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment