A minimal web application built with FastAPI and the Vonage Verify API. Users can request a OTP password code sent to their email address. Upon entering the correct code into the app, users are rewarded with an animated gif.
- OTP Code Generation: Generate a OTP code
- OTP Code Email Delivery: Use email to receive a OTP code
- Verification: Verify that the code provided by the user matches the code generated by Vonage
- Python 3.8+
- A Vonage API account
You will need a Vonage API account -- it's free to sign up!
Create your Verify application in the developer dashboard by navigating to the Applications window from the left hand menu and clicking the “Create new application” button. This will open the application creation menu. Give your application a human-friendly name like vonage-hello-world.
Under the Capabilities section, toggle the option for Verify.
Click the “Generate new application” button.
virtuanlenv venv && source venv/bin/activate
pip install -r requirements.txt
In the developer dashboard Applications menu, click on your application and then click Edit. Once the edit window opens, click on the button that says, “Generate public and private key”. This will trigger a download of your private key as a file with the extension .key. Keep this file private and do not share it anywhere it could be compromised.
Click on the "Save changes button" and also note your Application ID.
Move your private key file to your project directory and configure the variables in the .env_template file accordingly:
| Variable name | Variable value |
|---|---|
| VONAGE_APPLICATION_ID | This is the Vonage-generated ID of the Voice application you created for this sample code |
| VONAGE_PRIVATE_KEY_PATH | This is the path to the private.key file you downloaded from the developer dashboard |
Then update the name of the file from .env_template to .env.
To spin up the app, run the following:
fastapi dev
In your browser, navigate to http://127.0.0.1:8000. You should see a webpage inviting you to "Try out the Vonage Verify
API by providing your email" with a text field for your email address.
You should be redirected to a page that asks you to provide the code sent to your email. Check your email for a OTP code from verify[at]vonage.com and enter this code.
If the code provided matches the code generated and sent by Vonage, then you'll be redirected to another page featuring an animated gif.
This repo also provides a demonstration comparing the use of Pydantic to create and validate a Verify API request vs. manually creating the request.
Please note that these tests don't use any mocks, so you will be making real API calls which may incur costs.
Run the tests with the following command:
python -m unittest -v
Running the tests should produce the following outcome:
test_with_pydantic (tests.test_main.TestMain.test_with_pydantic) ... ERROR
test_without_pydantic (tests.test_main.TestMain.test_without_pydantic) ... FAIL
What's important to note here is that the test using Pydantic errors out as opposed to failing. This is because Pydantic catches the incorrect request body parameters when the VerifyRequest object is created:
pydantic_core._pydantic_core.ValidationError: 1 validation error for EmailChannel
to
Input should be a valid string [type=string_type, input_value=678910, input_type=int]
For further information visit https://errors.pydantic.dev/2.13/v/string_type
The test without Pydantic fails because the result is a 422 and not a 202 which is the API response for invalid parameters:
AssertionError: 422 != 202 : Test without Pydantic failed with: 422. Expected: 202
This indicates that the Verify endpoint was called despite invalid parameters -- something that Pydantic caught before making a call to the API. This demonstrates how Pydantic validates models at creation and can help prevent wasted API calls.




