Skip to content

Instantly share code, notes, and snippets.

@brian-tex
Created November 26, 2025 18:47
Show Gist options
  • Select an option

  • Save brian-tex/6ab944e3442f50a05a78b4da54d80753 to your computer and use it in GitHub Desktop.

Select an option

Save brian-tex/6ab944e3442f50a05a78b4da54d80753 to your computer and use it in GitHub Desktop.
import requests
import tempfile
import subprocess
import os
def make_api_call_with_pfx(pfx_path, pfx_password, url):
"""
Makes an API call using a PFX certificate for mutual TLS authentication.
Args:
pfx_path (str): Path to the .pfx file.
pfx_password (str): Password for the PFX file.
url (str): API endpoint URL.
Returns:
requests.Response: The HTTP response object.
"""
# Create temporary files for PEM certificate and key
cert_file = tempfile.NamedTemporaryFile(delete=False)
key_file = tempfile.NamedTemporaryFile(delete=False)
try:
# Extract certificate from PFX
subprocess.run([
"openssl", "pkcs12",
"-in", pfx_path,
"-clcerts", "-nokeys",
"-out", cert_file.name,
"-passin", f"pass:{pfx_password}"
], check=True)
# Extract private key from PFX
subprocess.run([
"openssl", "pkcs12",
"-in", pfx_path,
"-nocerts", "-nodes",
"-out", key_file.name,
"-passin", f"pass:{pfx_password}"
], check=True)
# Make the API request
response = requests.get(url, cert=(cert_file.name, key_file.name))
return response
finally:
# Clean up temporary files
os.unlink(cert_file.name)
os.unlink(key_file.name)
if __name__ == "__main__":
# Example usage
PFX_PATH = "your_certificate.pfx"
PFX_PASSWORD = "your_pfx_password"
API_URL = "https://api.example.com/endpoint"
resp = make_api_call_with_pfx(PFX_PATH, PFX_PASSWORD, API_URL)
print("Status Code:", resp.status_code)
print("Response Body:", resp.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment