This topic describes how to use a developer tool, such as Alibaba Cloud CLI, OpenAPI Explorer, and an Alibaba Cloud SDK, to call the Alibaba Cloud DNS API.

Use Alibaba Cloud CLI to call the Alibaba Cloud DNS API

Before you use this method, make sure that Alibaba Cloud CLI is installed.

The following examples show you how to use Alibaba Cloud CLI to call the AddDomainRecord, UpdateDomainRecord, DescribeDomainRecords, and DescribeSubDomainRecords operations. Before you call an API operation, you must understand related instructions in the topics in API Reference and obtain the values of the required request parameters. If an error occurs, you can view troubleshooting suggestions in the related topic.

  1. Call the AddDomainRecordd operation to create a DNS record.
  2. Call the UpdateDomainRecord operation to modify a DNS record. In this example, the hostname is changed from apitest3 to apitest4.
  3. Call the DescribeDomainRecords operation to query all the DNS records of the domain name ddnstest.top.
  4. Call the DescribeSubDomainRecords operation to query all the DNS records of the subdomain apitest.dns-example.com.
  5. Call the DescribeDomainRecordInfo operation to query the details of a DNS record.

Use OpenAPI Explorer to call the Alibaba Cloud DNS API

The following example shows you how to use OpenAPI Explorer to call the AddDomainRecord operation. For more information about OpenAPI Explorer, see What is OpenAPI Explorer?.

The following list describes the five Alibaba Cloud DNS API operations that are most frequently called:

  1. Call the AddDomainRecord operation to create a DNS record.
  2. Call the UpdateDomainRecord operation to modify a DNS record.
  3. Call the DescribeDomainRecords operation to query the DNS records of a domain name.
  4. Call the DescribeSubDomainRecords operation to query the DNS records of a subdomain.
  5. Call the Querying information about a DNS record operation to query the details of a DNS record.

The following figures show how to call the AddDomainRecord operation and the sample result.

Use OpenAPI ExploreCall result

Use the SDK for Java to call the Alibaba Cloud DNS API

The following example shows you how to use Alibaba Cloud DNS SDK for Java to call the AddDomainRecord operation. For information about how to configure Alibaba Cloud DNS SDKs, see SDKs.

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.3.0");
        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());
        }
    }
}     

Use the SDK for Node.js to call the Alibaba Cloud DNS API

The following example shows you how to use Alibaba Cloud DNS 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.3.0"
}

var requestOption = {
  method: 'POST'
};

client.request('AddDomainRecord', params, requestOption).then((result) => {
  console.log(JSON.stringify(result));
}, (ex) => {
  console.log(ex);
})     

Use the SDK for Go to call the Alibaba Cloud DNS API

The following example shows you how to use Alibaba Cloud DNS 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.3.0"
    request.Type = "A"
    request.RR = "apitest1"
    request.DomainName = "dns-example.com"

    response, err := client.AddDomainRecord(request)
    if err != nil {
        fmt.Print(err.Error())
    }
    fmt.Printf("response is %#v\n", response)
}  

Use the SDK for PHP to call the Alibaba Cloud DNS API

The following example shows you how to use Alibaba Cloud DNS 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.3.0",
                                        ],
                                    ])
                          ->request();
    print_r($result->toArray());
} catch (ClientException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
}       

Use the SDK for Python to call the Alibaba Cloud DNS API

The following example shows you how to use Alibaba Cloud DNS 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.3.0")
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'))     

Use the SDK for .NET to call the Alibaba Cloud DNS API

The following example shows you how to use Alibaba Cloud DNS 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.3.0";
            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);
            }
        }
    }
}      

Use the SDK for Ruby to call the Alibaba Cloud DNS API

The following example shows you how to use Alibaba Cloud DNS 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.3.0"
},
  opts: {
    method: 'POST'
  }
)

print response