All Products
Search
Document Center

Alibaba Cloud DNS:DNS API Quick Start

Last Updated:Dec 16, 2025

This topic describes how to call DNS APIs using developer tools such as Alibaba Cloud CLI, OpenAPI Explorer, and Alibaba Cloud SDKs.

Example: Call API operations using Alibaba Cloud CLI

Before you call an API, make sure that you have installed and configured Alibaba Cloud CLI.

This example uses Alibaba Cloud CLI to complete the following tasks:

  • Add a DNS record (AddDomainRecord)

  • Modify a DNS record (UpdateDomainRecord)

  • Query DNS records (DescribeDomainRecords)

  • Query DNS records for a subdomain (DescribeSubDomainRecords)

  • Query the details of a specific DNS record (DescribeDomainRecordInfo)

For instructions and required request parameters, see the API documentation. If an API call fails, see the corresponding API documentation for troubleshooting suggestions.

  1. Add a DNS record

  2. Modify a DNS record

  3. Query DNS records

  4. Query DNS records for a subdomain

  5. Query the details of a DNS record

Example: Call an API operation using OpenAPI Explorer

This example describes how to use OpenAPI Explorer to call the AddDomainRecord operation. For more information about OpenAPI Explorer, see What is OpenAPI Explorer?.

Test the AddDomainRecord call

image.png

image.png

SDK call examples

Example: Call an API using Java

This example describes how to use the Alibaba Cloud SDK for Java to call the AddDomainRecord operation. For more information about how to configure the DNS SDK, see the SDK Reference document.

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.*;
import com.aliyuncs.alidns.model.v20150109.*;
public class AddDomainRecord {
    public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
        IAcsClient client = new DefaultAcsClient(profile);
        AddDomainRecordRequest request = new AddDomainRecordRequest();
        request.setValue("3.0.X.X");
        request.setType("A");
        request.setRR("apitest");
        request.setDomainName("dns-example.com");
        try {
            AddDomainRecordResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }
}     

Example: Call an API using Node.js

This example describes how to use the Alibaba Cloud SDK for Node.js to call the AddDomainRecord operation.

const Core = require('@alicloud/pop-core');
var client = new Core({
  accessKeyId: '<accessKeyId>',
  accessKeySecret: '<accessSecret>',
  endpoint: 'https://alidns.aliyuncs.com',
  apiVersion: '2015-01-09'
});
var params = {
  "DomainName": "dns-example.com",
  "RR": "apitest1",
  "Type": "A",
  "Value": "3.0.X.X"
}
var requestOption = {
  method: 'POST'
};
client.request('AddDomainRecord', params, requestOption).then((result) => {
  console.log(JSON.stringify(result));
}, (ex) => {
  console.log(ex);
})     

Example: Call an API using Go

This example describes how to use the Alibaba Cloud SDK for Go to call the AddDomainRecord operation.

package main
import (
    "fmt"
    "github.com/aliyun/alibaba-cloud-sdk-go/services/alidns"
)
func main() {
    client, err := alidns.NewClientWithAccessKey("cn-hangzhou", "<accessKeyId>", "<accessSecret>")
    request := alidns.CreateAddDomainRecordRequest()
    request.Scheme = "https"
    request.Value = "3.0.X.X"
    request.Type = "A"
    request.RR = "apitest1"
    request.DomainName = "dns-example.com"
    response, err := client.AddDomainRecord(request)
    if err != nil {
        fmt.Print(err.Error())
        return
    }
    fmt.Printf("response is %#v\n", response)
}  

Example: Call an API using PHP

This example describes how to use the Alibaba Cloud SDK for PHP to call the AddDomainRecord operation.

<?php
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
// Download URL: https://github.com/aliyun/openapi-sdk-php
// User guide: https://github.com/aliyun/openapi-sdk-php/blob/master/README.md
AlibabaCloud::accessKeyClient('<accessKeyId>', '<accessSecret>')
                        ->regionId('cn-hangzhou') // replace regionId as you need
                        ->asDefaultClient();
try {
    $result = AlibabaCloud::rpc()
                          ->product('Alidns')
                          // ->scheme('https') // https | http
                          ->version('2015-01-09')
                          ->action('AddDomainRecord')
                          ->method('POST')
                          ->options([
                                        'query' => [
                                          'DomainName' => "dns-example.com",
                                          'RR' => "apitest1",
                                          'Type' => "A",
                                          'Value' => "3.0.X.X",
                                        ],
                                    ])
                          ->request();
    print_r($result->toArray());
} catch (ClientException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
}       

Example: Call an API using Python

This example describes how to use the Alibaba Cloud SDK for Python to call the AddDomainRecord operation.

#!/usr/bin/env python
#coding=utf-8
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkalidns.request.v20150109.AddDomainRecordRequest import AddDomainRecordRequest
client = AcsClient('<accessKeyId>', '<accessSecret>', 'cn-hangzhou')
request = AddDomainRecordRequest()
request.set_accept_format('json')
request.set_Value("3.0.X.X")
request.set_Type("A")
request.set_RR("apitest1")
request.set_DomainName("dns-example.com")
response = client.do_action_with_exception(request)
# python2:  print(response) 
print(str(response, encoding='utf-8'))     

Example: Call an API using .NET

This example describes how to use the Alibaba Cloud SDK for .NET to call the AddDomainRecord operation.

using System;
using System.Collections.Generic;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Alidns.Model.V20150109;
namespace AlidnsDemo
{
    class Program{
        static void Main(string[] args){
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
            DefaultAcsClient client = new DefaultAcsClient(profile);
            var request = new AddDomainRecordRequest();
            request.Value = "3.0.X.X";
            request.Type = "A";
            request.RR = "apitest1";
            request.DomainName = "dns-example.com";
            try {
                var response = client.GetAcsResponse(request);
                Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
            }
            catch (ServerException e){
                Console.WriteLine(e);
            }
            catch (ClientException e){
                Console.WriteLine(e);
            }
        }
    }
}      

Example: Call an API using Ruby

This example describes how to use the Alibaba Cloud SDK for Ruby to call the AddDomainRecord operation.

# gem install aliyunsdkcore
require 'aliyunsdkcore'
client = RPCClient.new(
  access_key_id:     '<accessKeyId>',
  access_key_secret: '<accessSecret>',
  endpoint: 'https://alidns.aliyuncs.com',
  api_version: '2015-01-09'
)
response = client.request(
  action: 'AddDomainRecord',
  params: {
    "DomainName": "dns-example.com",
    "RR": "apitest1",
    "Type": "A",
    "Value": "3.0.X.X"
},
  opts: {
    method: 'POST'
  }
)
print response

For more information about the API operations, see API overview.