Porkbun DNS: create DNS records via API using curl

Porkbun is an ICANN accredited domain name registrar with value pricing and an API that allows domain record creation and updates, which makes it especially useful for scripting.

This article assumes you already have an account, and have Porkbun managing at least one domain name.

Create API Access key

As a prerequisite, generate an API access key at the account level.  Here is their detailed documentation with screenshots showing the exact steps.

If your account does not yet have a verified email address, a link to Account>Settings will be shown before you can generate the API key.

Enable API access for managed domain

From the Account>Domain Management page, manually click down into the ‘Details’ of your purchased domain and enable “API Access”.

There is a screenshot of this in their detailed documentation.

List the managed domains

Export the API key and secret generated earlier as environment variables to be used by the upcoming scripts.

export API_KEY=pk1_your_key
export SECRET_KEY=sk1_your_secret

This call to ‘domain/listAll‘ will show all the domains currently being managed AND that have API access enabled.

curl --silent --header "Content-Type: application/json" \
     --request POST \
     --data "{
       \"apikey\": \"$API_KEY\",
       \"secretapikey\": \"$SECRET_KEY\",
       \"includeLabels\": \"yes\"
     }" \
     "https://api.porkbun.com/api/json/v3/domain/listAll" | jq '.domains[].domain' -r

List records of a managed domain

Use one of these domain names to query for all the records being managed under it.  A call to “/dns/retrieve” will return all record types: NS, A, ALIAS, TXT, etc.

the_domain="nameofmydomain.org"
curl --silent --header "Content-Type: application/json" \
     --request POST \
     --data "{
       \"apikey\": \"$API_KEY\",
       \"secretapikey\": \"$SECRET_KEY\"
     }" \
     "https://api.porkbun.com/api/json/v3/dns/retrieve/$the_domain" | jq .

Create a record

To create a new record, make a call to “dns/create” with the name and value of the new record.

new_record="mytest"
curl --silent --header "Content-Type: application/json" \
     --request POST \
     --data "{
       \"apikey\": \"$API_KEY\",
       \"secretapikey\": \"$SECRET_KEY\",
       \"name\" : \"$new_record\",
       \"type\" : \"A\",
       \"content\" : \"1.2.3.4\"
     }" \
     "https://api.porkbun.com/api/json/v3/dns/create/$the_domain" | jq .

This can be verified by querying all the managed records again, and filtering for the full record name.

curl --silent --header "Content-Type: application/json" \
     --request POST \
     --data "{
       \"apikey\": \"$API_KEY\",
       \"secretapikey\": \"$SECRET_KEY\"
     }" \
     "https://api.porkbun.com/api/json/v3/dns/retrieve/$the_domain" | jq ".records[] | select(.name==\"$new_record.$the_domain\") | .name" -r

Delete a record

A record can be deleted by calling the “dns/deleteByNameType” endpoint.

curl --silent --header "Content-Type: application/json" \
     --request POST \
     --data "{
       \"apikey\": \"$API_KEY\",
       \"secretapikey\": \"$SECRET_KEY\"
     }" \
     "https://api.porkbun.com/api/json/v3/dns/deleteByNameType/$the_domain/A/$new_record" | jq .

 

REFERENCES

porkbun.com, API docs

porkbun docs, Getting started with the Porkbun API

lowendbox.com blog, command line DNS using Porkbun API in scripts

pypi porkbun-api for python