SendSafely Python Client API
The SendSafely Client API allows programmatic access to SendSafely, providing a layer of abstraction over the complex REST API for secure data transfer capabilities within Python applications. It handles cryptographic details, file segmentation, and server authentication. The current version is 1.0.9.6, and the library appears to be actively maintained with frequent point releases.
Common errors
-
ModuleNotFoundError: No module named 'imghdr'
cause The `imghdr` module, previously used by the `pgpy` dependency, was removed from the Python standard library in Python 3.13.fixUpgrade the `sendsafely` library to version 1.0.9.6 or later, which contains a fix for Python 3.13 compatibility. -
sendsafely.exceptions.APIError: {'errorCode': 401, 'message': 'Unauthorized'}cause This usually indicates incorrect or missing `API_KEY`, `API_SECRET`, or an invalid `BASE_URL` during client initialization. It can also occur if the API key lacks necessary permissions or if a recipient is not authorized for a package.fixDouble-check your `BASE_URL`, `API_KEY`, and `API_SECRET` values. Ensure they are correct and have the necessary permissions for the operations you are performing. Verify the recipient email if applicable. Generate new credentials if unsure. -
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))cause This error often points to network connectivity issues, an incorrect `BASE_URL`, or problems with the SendSafely server being unreachable or refusing the connection.fixVerify your internet connection and that the `BASE_URL` is correct and accessible. Ensure no firewalls or proxies are blocking the connection. If the URL is correct, the SendSafely service might be temporarily unavailable. Consider adding retry logic with exponential backoff for transient network issues. -
package = sendsafely.Package(sendsafely_client) Argument missing: 'sendsafely_instance'
cause The `Package` class constructor requires an instance of the `SendSafely` client to be passed as its first argument (`sendsafely_instance`).fixInstantiate `Package` by providing an initialized `SendSafely` object: `package = Package(sendsafely_client)`.
Warnings
- breaking The `sendsafely` library (specifically its `pgpy` dependency) encountered a `ModuleNotFoundError: No module named 'imghdr'` when run on Python 3.13 due to the removal of `imghdr` from the standard library.
- gotcha Full access to all SendSafely API features, especially those for enterprise search and advanced workflows (e.g., SendSafely Actions), may require a SendSafely Enterprise subscription. Functionality can be limited for other account types.
- gotcha Directly interacting with the SendSafely REST API requires manual implementation of complex cryptographic operations (e.g., PGP encryption, key generation, timestamp and signature formatting). The Python Client API handles these complexities automatically.
- gotcha API Keys and API Secrets for authentication must be generated from the 'API Keys' section of your profile page when logged into your SendSafely portal. These credentials are distinct from user login credentials and are essential for programmatic access.
Install
-
pip install sendsafely
Imports
- SendSafely
from sendsafely import SendSafely, Package
- Package
package = sendsafely.Package()
from sendsafely import SendSafely, Package
Quickstart
import os
from sendsafely import SendSafely, Package
# Retrieve credentials from environment variables for security
BASE_URL = os.environ.get('SENDSAFELY_BASE_URL', 'https://your-company.sendsafely.com')
API_KEY = os.environ.get('SENDSAFELY_API_KEY', 'YOUR_API_KEY')
API_SECRET = os.environ.get('SENDSAFELY_API_SECRET', 'YOUR_API_SECRET')
RECIPIENT_EMAIL = os.environ.get('SENDSAFELY_RECIPIENT_EMAIL', 'user@example.com')
if not all([BASE_URL, API_KEY, API_SECRET, RECIPIENT_EMAIL]):
print("Please set SENDSAFELY_BASE_URL, SENDSAFELY_API_KEY, SENDSAFELY_API_SECRET, and SENDSAFELY_RECIPIENT_EMAIL environment variables.")
exit(1)
try:
# Create a SendSafely instance object for authentication
sendsafely_client = SendSafely(BASE_URL, API_KEY, API_SECRET)
# Create a new package
package = Package(sendsafely_client)
# Add a secure message to the package
package.encrypt_and_upload_message("Hello this is a secure message from Python.")
# Add a recipient to the package
package.add_recipient(RECIPIENT_EMAIL)
# Finalize the package to generate the secure link
response = package.finalize()
if response and response.get('secureLink'):
print(f"Secure Package Link: {response['secureLink']}")
else:
print("Failed to finalize package or retrieve secure link.")
print(response)
except Exception as e:
print(f"An error occurred: {e}")