Apidog

All-in-one Collaborative API Development Platform

API Design

API Documentation

API Debugging

API Mocking

API Automated Testing

Postman: How to Generate Random Phone Numbers Starting with 09/07/01

Struggling to test phone number functionalities? Postman's here to help! Generate random phone numbers starting with 09/07/01 for seamless testing. This guide unveils the code to create these numbers and integrate them into your Postman requests.

David Demir

David Demir

Updated on November 29, 2024

When testing APIs, there is often a need to pass phone numbers to the server-side. So, how can we generate random phone numbers and pass them to the API server-side using Postman, a tool for API testing? In this article, we will provide a detailed explanation of the process.

đź’ˇ
If you want to test your API with a more convenient way to generate phone numbers, Apidog has a built-in "data generator" tool that randomly generates phone numbers and other data.

With easy random data generation, find out just what other features Apidog has to offer by clicking the button below.
Apidog An integrated platform for API design, debugging, development, mock, and testing
REAL API Design-first Development Platform. Design. Debug. Test. Document. Mock. Build APIs Faster & Together.
button

If you want a more convenient way to generate phone numbers and test APIs, Apidog offers a built-in "Data Generator" tool that allows you to easily generate various types of data, including phone numbers, randomly. Apidog makes it easier to generate random data without writing scripts, making it more convenient than Postman.

Why Pass Random Phone Numbers in API Testing?

There are several typical use cases where phone numbers need to be passed to an API during testing:

  1. User Registration/Profile Creation
    When users register on a website or mobile app, they are often required to provide a phone number as a mandatory field. In this case, the phone number needs to be sent to the server and stored in the user's profile.
  2. Identity Verification/Two-Factor Authentication
    For financial services or important transactions, phone numbers may be used for identity verification. One-time passwords can be sent via SMS, or voice guidance authentication can be conducted over the phone.
  3. Shipping/Delivery Address Confirmation
    When purchasing products on e-commerce sites, customers may be asked to provide a phone number along with the delivery address. This is used by the shipping carrier to contact the recipient if necessary during delivery.
  4. Marketing and Promotions
    Some companies collect customer phone numbers for marketing purposes, such as sending promotional SMS messages, conducting phone surveys, or making sales calls.
  5. Emergency Contact Registration
    Phone numbers may be required in various situations, such as registering emergency contacts during job applications or online events.

By using random phone numbers during API testing, you can simulate real-world use cases and conduct more realistic tests.

How to Generate Random Phone Numbers in Postman?

Postman is a popular tool for API development and testing. It allows developers to create HTTP requests and send them to API endpoints, testing the API's behavior and inspecting the request and response data.

To generate random phone numbers in Postman when testing APIs, you can use test scripts.

What is Postman (A Tutorial for Beginners)
What is Postman? This article will provide a comprehensive introduction to Postman, so let’s get started.

Step-by-Step Guide to Generating Random Phone Numbers in Postman

Postman has a "Pre-request Script" feature that runs before sending a request. Here, you can use JavaScript to generate a random phone number and store it in an environment variable.

Step 1: Open Postman and create a new HTTP request.

Create a new HTTP request

Step 2: Enter the required endpoint URL and parameters for the request, then switch to the "Pre-request Script" and paste the following script:

// Define the phone number format
const phoneNumberFormat = '090-####-####';

// Function to generate random digits, also defining how many digits to generate
function getRandomDigit() {
  return Math.floor(Math.random() * 11);
}

// Function to generate a phone number
function generatePhoneNumber(format) {
  let phoneNumber = '';
  for (let i = 0; i < format.length; i++) {
    const char = format[i];
    if (char === '#') {
      phoneNumber += getRandomDigit();
    } else {
      phoneNumber += char;
    }
  }
  return phoneNumber;
}

// Set the random phone number as an environment variable
pm.environment.set('randomPhoneNumber', generatePhoneNumber(phoneNumberFormat));

This script generates a random 11-digit phone number starting with 090. The generated phone number is stored in the {{randomPhoneNumber}} variable.

postman generate random phone number

Step 3: In the request parameters, you can reference the {{randomPhoneNumber}} variable to pass the generated phone number to the API server-side.

postman send random phone number

Open the Postman "Console" to confirm that the generated phone number has been passed.

postman generated phone number past

A More Convenient Way to Pass Random Phone Numbers with Apidog

Instead of manually writing test scripts in Postman to generate and pass random phone numbers, Apidog provides a more convenient solution with its built-in Data Generator tool.

button

With Apidog's Data Generator tool, you can easily generate data, including phone numbers, based on your needs when testing APIs.

Step 1: Open Apidog and create a new HTTP request.

Step 2: When entering parameter values, click the "magic wand" icon to open the Data Generator tool. Select "Phone Number" as the data type to generate.

apidog generate phone number

Step 3: Click the "Send" button, and you can confirm that a random phone number has been successfully generated and passed by switching to the "Actual Request" tab.

By utilizing Apidog's Data Generator tool, you can easily generate and pass random phone numbers without manually writing test scripts, making it more convenient than Postman for testing APIs. Consider using Apidog as a tool for more efficient API testing.

Additionally, Apidog also supports the custom script, if this data generator tool of Apidog can not meet your requirements. You can add custom scripts in the "Post-processor" tab, and then fill in the same test script as Postman, then you can use variables to send the random phone number.

button

Conclusion

We have explained how to generate random phone numbers and pass them to the API server-side using Postman. In Postman, you can use JavaScript in the "Pre-request Script" to generate a random phone number and store it in an environment variable. Then, you can reference the generated phone number using the {{randomPhoneNumber}} format and include it in the request parameters.

Additionally, if you want a more convenient way to generate phone numbers and test APIs, Apidog offers a built-in "Data Generator" tool that allows you to randomly generate various types of data, including phone numbers. Apidog makes it easier to generate random data without writing scripts, making it more convenient than Postman.

Preparing appropriate test data is crucial for API testing. By generating and using random phone numbers, you can simulate real-world scenarios and conduct more realistic tests. Utilize tools like Postman and Apidog to perform efficient and comprehensive API testing.

Integrating AWS Secrets Manager with Apidog: Protecting API Sensitive DataTutorials

Integrating AWS Secrets Manager with Apidog: Protecting API Sensitive Data

Securing API keys and sensitive data is essential in API development. This guide walks you through integrating AWS Secrets Manager with Apidog, ensuring encrypted and efficient management of API secrets for improved security and smoother testing workflows.

Oliver Kingsley

December 24, 2024

Integrating HashiCorp Vault with Apidog: Secure API Keys, Tokens and MoreTutorials

Integrating HashiCorp Vault with Apidog: Secure API Keys, Tokens and More

Discover how to securely integrate HashiCorp Vault with Apidog. Learn to manage API secrets dynamically, automate workflows, and enhance security practices with this powerful combination. Follow the step-by-step guide to streamline your API development and testing.

Oliver Kingsley

December 23, 2024

GitHub Copilot Free: How to Get Started?Tutorials

GitHub Copilot Free: How to Get Started?

Discover how to start using GitHub Copilot Free, the AI-powered coding assistant. This guide covers setup steps for popular IDEs like VS Code and JetBrains, helping you unlock productivity with intelligent code suggestions and completions—at no cost!

Oliver Kingsley

December 19, 2024