Authentication overview

Plivo's API authentication system is designed to secure programmatic access to its SMS and Voice services. All interactions with the Plivo API require authentication to verify the identity of the client making the request and authorize access to account resources. This ensures that only legitimate applications can send messages, initiate calls, manage phone numbers, and retrieve account data. The primary authentication mechanism employed by Plivo is HTTP Basic Authentication, a widely adopted standard for securing web API interactions over HTTPS.

When you make an API request to Plivo, you provide your unique Auth ID and Auth Token. These credentials are used by Plivo's servers to identify your account and grant access based on your permissions. Proper management and protection of these credentials are paramount to maintaining the security of your Plivo account and preventing unauthorized usage of your communication services. All API requests must be made over HTTPS, which encrypts the communication channel and protects your credentials from interception during transit, a fundamental security practice for web APIs as outlined by the HTTP Basic Authentication specification.

Plivo provides client libraries (SDKs) in several popular programming languages, including Python, Ruby, PHP, Node.js, Java, .NET, and Go. These SDKs simplify the process of constructing authenticated requests by abstracting away the underlying HTTP Basic Authentication details, allowing developers to focus on integrating Plivo's communication features into their applications without manually handling header construction. The Plivo API documentation provides comprehensive guides and code examples for each supported SDK to assist developers in quickly getting started with authenticated requests.

Supported authentication methods

Plivo primarily supports one method for authenticating API requests:

  • HTTP Basic Authentication: This method involves sending your Auth ID as the username and your Auth Token as the password in the HTTP Authorization header for every API request. The combination of these two credentials uniquely identifies your Plivo account and authorizes the request. This is the standard and recommended method for accessing all Plivo API endpoints.

The use of HTTPS (TLS/SSL) is mandatory for all API requests to Plivo. This ensures that your Auth ID and Auth Token are encrypted during transmission, protecting them from eavesdropping and man-in-the-middle attacks. Without HTTPS, even Basic Authentication would be vulnerable to credential theft, as documented by Mozilla's web security guidelines.

Authentication methods summary

Method When to Use Security Level
HTTP Basic Authentication All programmatic access to Plivo APIs, initiated from server-side applications or secure environments. High (when combined with HTTPS)

Getting your credentials

Your Plivo Auth ID and Auth Token are essential for authenticating API requests. These credentials are automatically generated when you create a Plivo account. You can locate and manage them within your Plivo Console.

Steps to retrieve your Plivo credentials:

  1. Log in to your Plivo account: Navigate to the Plivo website and log in using your registered email address and password.
  2. Access the Dashboard: Once logged in, you will typically be directed to your account Dashboard.
  3. Locate Auth ID and Auth Token: On the Dashboard, your Auth ID and Auth Token are usually prominently displayed. The Auth ID is a unique identifier for your account, typically starting with 'MA'. The Auth Token is a long, randomly generated string that acts as your secret key. For security purposes, the Auth Token may be partially masked until you explicitly choose to reveal it.
  4. Copy your credentials: Carefully copy both your Auth ID and Auth Token. These should be treated as sensitive information, similar to passwords.
  5. Manage (optional): The Plivo Console also provides options to regenerate your Auth Token if you suspect it has been compromised or if you need to perform a security rotation. This action will invalidate the old token, requiring you to update your applications with the new token.

It is important to store your Auth ID and Auth Token securely and never hardcode them directly into publicly accessible client-side code. Best practices involve using environment variables, configuration management tools, or secure secret management services to store and retrieve these credentials in your server-side applications. The Plivo documentation provides further guidance on managing your account and credentials.

Authenticated request example

This section demonstrates how to make an authenticated API request to Plivo using cURL, a common command-line tool, and Python, leveraging one of Plivo's official SDKs. These examples illustrate the use of Auth ID and Auth Token for authentication.

cURL example (sending an SMS)

This cURL command sends an SMS message using the Plivo Messaging API. Replace <YOUR_AUTH_ID> and <YOUR_AUTH_TOKEN> with your actual credentials.

curl -i \ 
-H "Content-Type: application/json" \ 
-X POST https://api.plivo.com/v1/Account/<YOUR_AUTH_ID>/Message/ \ 
-d '{"src": "12015550123", "dst": "12025550123", "text": "Hello from Plivo!"}' \ 
-u <YOUR_AUTH_ID>:<YOUR_AUTH_TOKEN>

In this cURL example, the -u <YOUR_AUTH_ID>:<YOUR_AUTH_TOKEN> flag automatically constructs the Authorization: Basic header required for HTTP Basic Authentication. The URL includes the Auth ID as part of the path, as specified in the Plivo Message API documentation.

Python SDK example (sending an SMS)

Using the Plivo Python SDK simplifies the authentication process. After installing the SDK (pip install plivo), you can send an SMS as follows:

import plivo
import os

# Retrieve credentials from environment variables for security
AUTH_ID = os.environ.get('PLIVO_AUTH_ID')
AUTH_TOKEN = os.environ.get('PLIVO_AUTH_TOKEN')

if not AUTH_ID or not AUTH_TOKEN:
    print("Please set PLIVO_AUTH_ID and PLIVO_AUTH_TOKEN environment variables.")
    exit()

client = plivo.RestClient(AUTH_ID, AUTH_TOKEN)

try:
    response = client.messages.create(
        src='12015550123', # Sender's phone number
        dst='12025550123', # Recipient's phone number
        text='Hello from Plivo using Python SDK!'
    )
    print(response)
except plivo.exceptions.PlivoRestError as e:
    print(f"Error sending message: {e}")

The Python SDK's plivo.RestClient constructor automatically handles the encoding and inclusion of your Auth ID and Auth Token in the HTTP Authorization header for all subsequent API calls made through that client instance. This approach is generally recommended for production applications due to its ease of use and adherence to best practices for credential handling by using environment variables.

Security best practices

Securing your Plivo account and API access is critical to prevent unauthorized use of your communication services. Adhering to the following best practices will help maintain the integrity and confidentiality of your operations.

Credential management

  • Protect your Auth Token: Treat your Auth Token as you would a password. It grants full access to your Plivo account resources. Never share it publicly, commit it to version control systems (like Git), or hardcode it directly into client-side code (e.g., JavaScript running in a browser).
  • Use environment variables: Store your Auth ID and Auth Token in environment variables on your server or in secure configuration management systems. This prevents them from being exposed in your codebase and makes it easier to manage credentials across different environments (development, staging, production).
  • Rotate credentials regularly: Periodically regenerate your Auth Token from the Plivo Console, especially if you suspect it may have been compromised or as part of a routine security policy. This invalidates old tokens, forcing applications to use the new one.
  • Least privilege access: If Plivo introduces more granular access controls in the future, aim to configure API keys with the minimum necessary permissions required for the specific tasks they perform.

API access security

  • Always use HTTPS: Ensure all API requests to Plivo are made over HTTPS. Plivo enforces HTTPS, so attempts to use HTTP will fail. This encryption is vital for protecting your Auth ID and Auth Token during transit against eavesdropping and man-in-the-middle attacks, as mandated by W3C security recommendations.
  • IP Whitelisting (if available): If your application's infrastructure has static outbound IP addresses, consider configuring IP whitelisting in your Plivo account settings (if Plivo offers this feature). This restricts API access to only known IP addresses, adding an extra layer of security.
  • Monitor API usage: Regularly review your Plivo account activity and usage logs for any unusual patterns or unauthorized access attempts. Plivo's console typically provides tools for this monitoring.
  • Error handling: Implement robust error handling in your applications to gracefully manage authentication failures. Avoid exposing sensitive error details that could aid an attacker in identifying vulnerabilities.

Code and environment security

  • Secure development practices: Adhere to secure coding guidelines to prevent common vulnerabilities like injection attacks, which could potentially expose sensitive credentials.
  • Keep SDKs updated: Regularly update Plivo SDKs in your applications to benefit from the latest security patches and features.
  • Secure your development environment: Ensure your development machines and deployment pipelines are secure to prevent credential theft.

By implementing these security best practices, you can significantly reduce the risk of unauthorized access to your Plivo account and ensure the reliable and secure operation of your communication applications.