How to Create a new DNS record in Google domains with Python

It’s easy!

Here’s the code:

# Import the necessary libraries
import google.auth
from google.oauth2 import service_account
from googleapiclient.discovery import build

# Set up the necessary credentials for authenticating with the API
SCOPES = ['https://www.googleapis.com/auth/domains']
SERVICE_ACCOUNT_FILE = '/path/to/service_account_file.json'

credentials = service_account.Credentials.from_service_account_file(
    SERVICE_ACCOUNT_FILE, scopes=SCOPES
)

# Build the API client
client = build('domains', 'v1', credentials=credentials)

# Set the domain name and DNS record details
domain_name = 'example.com'
dns_record = {
    'ttl': 300,  # Time to live in seconds
    'type': 'A',  # DNS record type (e.g. A, CNAME, MX, etc.)
    'name': 'www',  # Subdomain name (e.g. 'www')
    'data': '1.2.3.4'  # IP address or other data for the DNS record
}

# Create the DNS record
response = client.dnsRecord.create(
    parent=f'customer/{domain_name}', body=dns_record
).execute()

# Print the response from the API
print(response)

Posted

in

, ,

by

Tags: