How to generate VAPID keys with Python

How to generate VAPID keys with Python

In this guide, we will see how to generate VAPID keys using Python.

The VAPID (Voluntary Application Server Identification) keys are used to authenticate servers that send push notifications to browsers via the Web Push protocol. These keys are essential to ensure the security and reliability of push notifications. In this guide, we will see how to generate VAPID keys using Python.

Before starting, make sure you have Python installed on your system. You can download Python from the official website. Additionally, you will need the py-vapid package, which provides the necessary tools to generate and manage VAPID keys.

To install py-vapid, you can use pip, Python's package manager. Open a terminal and run the following command:


pip install py-vapid

Once the package is installed, you can use the following Python script to generate a pair of VAPID keys (public key and private key).


from vapid import Vapid

# Generate a new pair of VAPID keys
vapid_keys = Vapid().generate_keys()

# Extract the public and private keys
public_key = vapid_keys["public_key"]
private_key = vapid_keys["private_key"]

# Print the generated keys
print("Public Key: ", public_key)
print("Private Key: ", private_key)

This script generates a new pair of VAPID keys and prints them in the terminal. The public key will be used to identify the server sending the push notifications, while the private key will be used to sign the messages.

Once the keys are generated, you can use them to send push notifications. Below is an example of how to use the VAPID keys with the pywebpush library to send a push notification.

To install pywebpush, run the following command:


pip install pywebpush

Here is an example of how to send a push notification using the generated VAPID keys:


from pywebpush import webpush, WebPushException

# Client subscription data
subscription_info = {
    "endpoint": "https://fcm.googleapis.com/fcm/send/...",
    "keys": {
        "p256dh": "base64-encoded-key",
        "auth": "base64-encoded-auth-secret"
    }
}

# Load the generated VAPID keys
vapid_private_key = "your_private_key"
vapid_claims = {
    "sub": "mailto:your_email@example.com"
}

# Message content
message = {
    "title": "Notification Title",
    "body": "Notification body",
    "icon": "icon_url"
}

# Sending the push notification
try:
    response = webpush(
        subscription_info,
        message,
        vapid_private_key=vapid_private_key,
        vapid_claims=vapid_claims
    )
    print("Notification sent successfully!")
except WebPushException as ex:
    print("Error sending notification: ", repr(ex))

In this example, subscription_info contains the client's subscription information, which should be obtained during the registration of the push notification service in the browser. vapid_private_key and vapid_claims are used to sign the message, ensuring it comes from an authenticated source.

Conclusion

Generating VAPID keys with Python is a straightforward process thanks to the py-vapid library. These keys are fundamental to ensuring the security of push notifications sent to browsers. With the generated keys, you can use pywebpush to send secure and reliable push notifications to your users.