dns-lexicon

3.23.2 · active · verified Fri Apr 17

dns-lexicon is a Python library that provides a standardized and agnostic way to manipulate DNS records across various DNS providers. It abstracts away provider-specific APIs, allowing users to manage DNS records programmatically or via a command-line interface. The library is actively maintained with frequent minor releases, typically on a monthly basis, adding new providers and fixing existing ones. The current version is 3.23.2.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a `Client` for a specific DNS provider (e.g., Cloudflare) using API credentials from environment variables or direct input, and then how to list existing DNS records for a domain. It also includes commented-out examples for creating and deleting records, which require careful use.

import os
from lexicon.client import Client

# Example for Cloudflare. Replace with your actual provider and credentials.
# Credentials should be stored securely, e.g., in environment variables.
provider_name = 'cloudflare'
domain_name = 'example.com' # Replace with your actual domain

config = {
    'provider_name': provider_name,
    'domain': domain_name,
    'cloudflare_token': os.environ.get('LEXICON_CLOUDFLARE_TOKEN', 'YOUR_CLOUDFLARE_API_TOKEN_HERE')
}

try:
    client = Client(config)
    print(f"Connected to {provider_name} for domain {domain_name}")

    # List all records
    records = client.list_records()
    print(f"Existing records for {domain_name}:")
    for record in records:
        print(f"  ID: {record['id']}, Name: {record['name']}, Type: {record['type']}, Content: {record['content']}")

    # Example: Create a TXT record (uncomment to run)
    # record_name = 'test-record'
    # record_content = 'This is a test TXT record from dns-lexicon'
    # new_record = client.create_record(rtype='TXT', name=record_name, content=record_content)
    # print(f"Created TXT record: {new_record['name']} -> {new_record['content']}")

    # Example: Delete a record by ID (uncomment and replace with actual ID)
    # record_id_to_delete = 'your_record_id_here'
    # client.delete_record(identifier=record_id_to_delete)
    # print(f"Deleted record with ID: {record_id_to_delete}")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →