Skip to content

Vonage-Community/demo-verify-python-pydantic-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Email Verification Web App With FastAPI and Vonage

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.

Features

  • 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

How To Get This Code Running

Prerequisites

Setup

1. Create a Vonage account

You will need a Vonage API account -- it's free to sign up!

2. Create a Verify application

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.

A screenshot of the Create an application menu in the Vonage developer dashboard.

Run the code

1. Create and activate a Python virtual environment

virtuanlenv venv && source venv/bin/activate

2. Install dependencies

pip install -r requirements.txt

3. Obtain your Vonage secrets

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.

A screenshot of the application Edit menu in the Vonage developer dashboard showing where to generate a public and private key.

A screenshot of the Video application in the Vonage developer dashboard indicating where you can find the application ID.

4. Configure your environment variables

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.

5. Run the app

To spin up the app, run the following:

fastapi dev

6. Try it out!

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.

A screenshot of the initial email input page.

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.

A screenshot of the code verification page.

If the code provided matches the code generated and sent by Vonage, then you'll be redirected to another page featuring an animated gif.

Running the tests

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.

1. Run the tests

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 the tests tell us

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.

References

About

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.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors