This guide shows how to verify a user in a Python application using the Verification API and Python SDK to make and then report an SMS PIN verification.
The following diagram demonstrates the flow that happens to verify a user:
- The end user visits your website or service and tries to log in.
- Your backend then makes a request to the Sinch platform and initiates an SMS PIN verification request.
- The Sinch platform sends an SMS message with a one time PIN code to the phone number of the user.
- The user enters the code they received.
- Your backend makes a report verification request using the code the user entered.
- If the code matches, your backend will receive a success result from Sinch.
- The user, now verified, can proceed to log in to your site or service.
Before you can get started, you need the following already set up:
- Set all Verification API configuration settings.
- Python and a familiarity with how to create a new file.
- PIP (package installer for Python) and a familiarity with how to install Python modules.
- A mobile handset that can receive SMS messages.
Create a new project folder.
The easiest way to install the SDK is using pip
:
- Open a command prompt or terminal to the local repository folder.
- Execute the following command:
pip install sinch
In your project folder, create a new filed named "app.py" and paste the provided "app.py" code into the file.
# Use this code to make an SMS PIN verification request and then report the code using the Verification API and Python SDK.
from sinch import SinchClient
from sinch.domains.verification.models import VerificationIdentity
from sinch.domains.verification.exceptions import VerificationException
sinch_client = SinchClient(
application_key="YOUR_application_key",
application_secret="YOUR_application_secret"
)
def start_verification(phone_number):
response = sinch_client.verification.verifications.start_sms(
identity=VerificationIdentity(
type="number",
endpoint=phone_number
)
)
return response
def report_code(id, code):
response = sinch_client.verification.verifications.report_by_id(
id=id,
verification_report_request={
"code": code
}
)
return response
def main():
print("Enter a phone number to start verification or enter CTRL+C to quit.")
phone_number = input("Phone number: ")
try:
verification_response = start_verification(phone_number)
except VerificationException as err:
print("Invalid number? Check traceback information:")
raise
print("Enter a verification code or enter CTRL+C to quit.")
verification_code = input("Verification code: ")
try:
report_verification_code_response = report_code(
verification_response.id,
verification_code
)
except VerificationException as err:
print("Invalid number? Check traceback information:")
raise
print(report_verification_code_response)
if __name__ == "__main__":
main()
The code provided includes placeholder parameters. You'll need to update the parameters detailed in the following subsections with your values.
To start using the SDK, you need to initialize the main client class with your credentials from your Sinch dashboard and additionally add your Verification app credentials.
from sinch import SinchClient
sinch_client = SinchClient(
application_key="YOUR_application_key",
application_secret="YOUR_application_secret"
)
For testing purposes on your local environment it's fine to use hardcoded values, but before deploying to production we strongly recommend using environment variables to store the credentials, as in the following example:
import os
from sinch import SinchClient
sinch_client = SinchClient(
application_key=os.getenv("APPLICATION_KEY"),
application_secret=os.getenv("APPLICATION_SECRET")
)
Save the file.
Now you can execute the code and initiate your verification request. Run the following command:
python app.py
Follow the prompts in the console and enter the phone number of the mobile handset to which you want to send the SMS PIN verification request.
You should receive a text message to your mobile handset with a verification code. In a production scenario, this is the code that a user would enter into your app to verify their account.
Troubleshooting tipIf after running your app you receive a 5000 error response, you may have forgotten to save your file after adding your authentication values. This is an easy mistake to make! Try saving the file and running the app again.
You should also see the JSON response in the console.
Now that you've received the SMS PIN code to your mobile handset, it's time to report that code to the Sinch servers to complete the verification process.
In the console, follow the prompts by entering the code you received on your mobile handset.
If you entered the code that you received, you should see a success report response in your console.
Enter
Q
in the console to quit the application or try sending a code to your mobile handset again and reporting an incorrect code to see what happens!