Send a Webmention in 10 (or fewer) lines of Python code
Published on under the IndieWeb category.Toggle Memex mode

Webmentions enable distributed social interactions over the web. Using webmentions, you can send replies, likes, and other interactions that were published on your site to respond to another web page. For example, I send Webmentions for all of the bookmarks I create on my website. If a site can receive Webmentions, they will be notified that I bookmarked their post for later.
Webmentions work by sending a notification to a special server on a site called a Webmention endpoint. The endpoint will process the mention, make sure it is valid, then keep track of the mention for the site author to see. For a Webmention to be valid, there must be a link between the source (the page that mentions a page), and the target (the page that is going to receive the mention).
The code
With help from the IndieWeb Utils Python library, a library which I have helped make, you can send a Webmention in ten lines of Python code.
import indieweb_utils
source = "https://jamesg.blog"
target = "https://jamesg.blog/2022/10/12/indieweb-utils-v-0-4-0/"
try:
response = indieweb_utils.send_webmention(source, target)
except indieweb_utils.webmentions.send.CouldNotConnectToWebmentionEndpoint as exception:
raise exception
except indieweb_utils.webmentions.send.GenericWebmentionError as exception:
raise exception
print(f"Your webmention from {source} to {target} was sent successfully!")
This code imports the IndieWeb Utils Python library and invokes the send_webmention
function. This code sends a Webmention to my blog home page to notify me that one of my articles linked to my home page. You can read more about how Webmention works on the IndieWeb wiki Webmention page.
The code prints a message to the console if my Webmention cannot be sent. This works by catching errors the IndieWeb Utils library throws when a Webmention could not be successfully sent. These errors will include a statement to help you understand why a Webmention was not sent. If my Webmention is sent successfully, a message is printed to the console informing me that the Webmention was received.
Conclusion
The above code shows that, with minimal effort, you can add Webmention sending as part of a Python application with minimal effort. You could use the code above to send Webmentions to all of the sites you link to in your blog posts. You could use the logic to send notifications to sites when you bookmark one of their posts. You could use the code above in a social publishing platform to send notifications to someone when their site was mentioned on your platform. There are a lot of potential options!
If you use the solution above, let me know! I'd love to get your thoughts on your experience and whether there are opportunities to improve this function.
Tagged in indieweb-utils, python, indieweb.
Responses
Comment on this post
Respond to this post by sending a Webmention.
Have a comment? Email me at readers@jamesg.blog.