SMS verification for individual developers

更新时间:
复制 MD 格式

This article explains how individual developers can use the Alibaba Cloud SMS Authentication service to quickly add sms verification code functionality without a business license. It covers service activation, API calls, using pre-configured resources, and security best practices. This guide applies to various scenarios, including web applications, apps, and mini programs.

1. Alibaba Cloud SMS Authentication service

Note

Applicable scenarios: This service is for individual developers who lack enterprise qualifications and need to add sms verification code functionality for user registration, login, or password recovery in a web application, app, or mini program.

The Alibaba Cloud SMS Authentication service allows users who have completed individual real-name verification to directly call APIs to send and check sms verification codes, without needing to apply for qualifications, a signature, or a template. The system provides pre-configured signatures and standard verification code templates to simplify the integration process, making it ideal for agile development and personal projects.

Understand the differences between the SMS Authentication service and the standard sms verification code service:

The standard sms verification code service requires enterprise qualifications and is unavailable to individual developers. For the integration method, the SMS Authentication service supports API, SDK, http protocol, and the console; the standard service supports all but the console. For the billing method, neither service charges for failed sends.

2. Integration steps

Prerequisites

  1. You have an Alibaba Cloud account that has completed individual real-name verification.

  2. You have activated the SMS Authentication feature in the Phone Number Verification Service console .

  3. You have created an AccessKey ID and AccessKey Secret for API calls. We recommend using the AccessKey of a RAM user. For more information, see Create an AccessKey.

Service features

  • No qualifications required: No business license, signature application, or template application is needed.

  • System-provided resources: The platform provides a system-provided signature and a standard verification code template. These are the only available signature and template options. Custom creation is not supported.

  • Cross-platform compatibility: Supports API calls from multiple platforms, including web applications, apps, and mini programs.

  • No charge for failed sends: You are not charged for failed messages.

Signature and template restrictions

  • The system-provided signature and template must be used together. Pairing a custom signature with a system-provided template, or a system-provided signature with a custom template, is not supported.

  • The content of a system-provided signature cannot be added to or modified.

  • Due to carrier policy restrictions, custom signatures and templates are not supported.

Integration workflow

image

For detailed steps, see: SMS Authentication service user guide.

Install the SDK

The code examples in this article use the Alibaba Cloud Phone Number Verification Service SDK for Python. Run the following command to install it:

pip install alibabacloud_dypnsapi20170525

Note

If you use Python 3, replace pip with pip3.

3. Code examples

The following examples use Python. For other languages and detailed parameter descriptions, see the SendSmsVerifyCode and CheckSmsVerifyCode API references.

The Phone Number Verification Service SDK supports the following languages:

Major languages

GitHub address

Latest SDK download link

Java

java-dypnsapi-20170525

Download Java SDK

Python

python-dypnsapi-20170525

Download Python SDK

PHP

php-dypnsapi-20170525

Download PHP SDK

Go

go-dypnsapi-20170525

Download Go SDK

Node.js

nodejs-dypnsapi-20170525

Download Node.js SDK

C++

cpp-dypnsapi-20170525

Download C++ SDK

C#

csharp-dypnsapi-20170525

Download C# SDK

Swift

swift-dypnsapi-20170525

Download Swift SDK

When you call the SendSmsVerifyCode API, the following parameters are required: phone_number (the mobile phone number that receives the verification code), sign_name (the system-provided signature name obtained from the Phone Number Verification Service console), and template_code (the system-provided verification code template number obtained from the Phone Number Verification Service console). Other parameters are optional. For more information, see the API documentation.

Send code

# -*- coding: utf-8 -*-
# This file is auto-generated, don't edit it. Thanks.
import os
import sys
from typing import List
from alibabacloud_dypnsapi20170525.client import Client as Dypnsapi20170525Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_dypnsapi20170525 import models as dypnsapi_20170525_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient
def create_client(
    access_key_id: str,
    access_key_secret: str,
) -> Dypnsapi20170525Client:
    """
    Initializes a client with an AccessKey ID and AccessKey Secret.
    @param access_key_id:
    @param access_key_secret:
    @return: Client
    @throws Exception
    """
    config = open_api_models.Config(
        # Required: Your AccessKey ID.
        access_key_id=access_key_id,
        # Required: Your AccessKey Secret.
        access_key_secret=access_key_secret
    )
    # The endpoint to access. For more information, see https://api.aliyun.com/product/Dypnsapi
    config.endpoint = f'dypnsapi.aliyuncs.com'
    return Dypnsapi20170525Client(config)
def main(
    args: List[str],
) -> None:
    # Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured.
    # Exposing AccessKeys in your code may lead to security risks. The following code demonstrates how to call the API by getting the AccessKeys from environment variables, which is for reference only.
    # We recommend using a more secure method, such as STS, for authentication. For more information, see https://help.aliyun.com/document_detail/378657.html.
    client = create_client(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
    send_sms_verify_code_request = dypnsapi_20170525_models.SendSmsVerifyCodeRequest(
        phone_number='your_value',
        sign_name='your_value',  # Obtain the system-provided signature name from the Phone Number Verification Service console. Custom creation is not supported.
        template_code='your_value',  # Obtain the system-provided template code from the Phone Number Verification Service console. Custom creation is not supported.
        scheme_name='your_value',
        template_param='your_value',
        country_code='your_value',
        sms_up_extend_code='your_value',
        out_id='your_value',
        interval=1,
        valid_time=1,
        return_verify_code=False,
        code_type=1,
        duplicate_check=False,
        early_warn=False
    )
    runtime = util_models.RuntimeOptions()
    try:
        # Print the API response.
        client.send_sms_verify_code_with_options(send_sms_verify_code_request, runtime)
    except Exception as error:
        # Print the error if needed.
        UtilClient.assert_as_string(error.message)
async def main_async(
    args: List[str],
) -> None:
    # Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured.
    client = create_client(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
    send_sms_verify_code_request = dypnsapi_20170525_models.SendSmsVerifyCodeRequest(
        phone_number='your_value',
        sign_name='your_value',  # Obtain the system-provided signature name from the Phone Number Verification Service console. Custom creation is not supported.
        template_code='your_value',  # Obtain the system-provided template code from the Phone Number Verification Service console. Custom creation is not supported.
        scheme_name='your_value',
        template_param='your_value',
        country_code='your_value',
        sms_up_extend_code='your_value',
        out_id='your_value',
        interval=1,
        valid_time=1,
        return_verify_code=False,
        code_type=1,
        duplicate_check=False,
        early_warn=False
    )
    runtime = util_models.RuntimeOptions()
    try:
        # Print the API response.
        await client.send_sms_verify_code_with_options_async(send_sms_verify_code_request, runtime)
    except Exception as error:
        # Print the error if needed.
        UtilClient.assert_as_string(error.message)
if __name__ == '__main__':
    main(sys.argv[1:])

Check code

import os
import sys
from alibabacloud_dypnsapi20170525.client import Client as Dypnsapi20170525Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_dypnsapi20170525 import models as dypnsapi_20170525_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient
def create_client(
    access_key_id,
    access_key_secret,
):
    config = open_api_models.Config(
        # Required: Your AccessKey ID.
        access_key_id=access_key_id,
        # Required: Your AccessKey Secret.
        access_key_secret=access_key_secret
    )
    # The endpoint to access.
    config.endpoint = f'dypnsapi.aliyuncs.com'
    return Dypnsapi20170525Client(config)
def main(
    args,
):
    client = create_client(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
    check_sms_verify_code_request = dypnsapi_20170525_models.CheckSmsVerifyCodeRequest(
        case_auth_policy=0,
        country_code='cn',
        out_id='your_out_id',
        phone_number='your_phone_number',
        verify_code='your_verify_code'
    )
    runtime = util_models.RuntimeOptions()
    try:
        # Print the API response.
        resp = client.check_sms_verify_code_with_options(check_sms_verify_code_request, runtime)
        print(resp)
    except Exception as error:
        # Print the error if needed.
        UtilClient.assert_as_string(error.message)
async def main_async(
    args,
):
    client = create_client(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
    check_sms_verify_code_request = dypnsapi_20170525_models.CheckSmsVerifyCodeRequest(
        case_auth_policy=0,
        country_code='cn',
        out_id='your_out_id',
        phone_number='your_phone_number',
        verify_code='your_verify_code'
    )
    runtime = util_models.RuntimeOptions()
    try:
        # Print the API response.
        resp = await client.check_sms_verify_code_with_options_async(check_sms_verify_code_request, runtime)
        print(resp)
    except Exception as error:
        # Print the error if needed.
        UtilClient.assert_as_string(error.message)
if __name__ == '__main__':
    main(sys.argv[1:])

4. FAQ

Q: Can I customize the verification code?

A: Yes. You can pass a custom 4- to 6-digit numeric verification code by using the Code parameter. However, note that when you use a custom verification code, the CheckSmsVerifyCode API does not support verifying it. You must implement your own logic to store and compare verification codes. We recommend using the system-generated verification code, which provides a complete code generation and verification workflow.

Q: Support for international numbers?

A: Currently, the SMS Authentication service only supports Chinese mainland mobile numbers (country code 86).

Q: How to view sending statistics?

A: Log in to the Phone Number Verification Service console. There, you can view metrics such as sent volume, number of verified messages, and verification rate for a specific time period.