Package: nic-api
NIC.RU API Python libraryThis library provides interfaces for managing DNS zones and records for the DNS
service NIC.RU (Ru-Center).InstallationUsingpip:pipinstallnic-apiIf you want to use this module in your project, addnic-apito its
dependencies.UsageInitializationTo start using the API, you should get an OAuth application login and a
password from NIC.RU. Here is the registration page:https://www.nic.ru/manager/oauth.cgi?step=oauth.app_registerCreate an instance ofnic_api.DnsApiand provide the obtained credentials:fromnic_apiimportDnsApiapp_login="your_application_login"app_password="your_application_secret"api=DnsApi(app_login,app_password)AuthenticationCall theDnsApi.get_token()method with the username and the password for
your NIC.RU account:api.get_token(username="Your_account/NIC-D",password="Your_password",)Now you are ready to use the API.Till the token is valid, you don't need to provide neither client username or
password to access the API – just create an instance of theDnsApiclass
with the same OAuth config, and pass the cached token astokenparameter:api=DnsApi(app_login,app_password,token)If you have a valid refresh token, you can get a new access token with it:api.refresh_token(refresh_token)You can add a callback method to save the token somewhere outside of theDnsApiobject. Use thetoken_updater_clbparameter for that.Viewing services and DNS zonesIn the NIC.RU, DNS zones are located in “services”:api.services()Usually there is only one service per account. To view available zones in the
serviceMY_SERVICEcallDnsApi.zones():api.zones("MY_SERVICE")Always check if there are any uncommitted changes in the zone before making
any modifications – your commit would apply all unsaved changes!Getting DNS recordsFor viewing or modifying records, you need to specify both service and DNS
zone name:api.records("MY_SERIVCE","example.com")Creating a recordTo add a record, create an instance of one of thenic_api.models.DNSRecordsubclasses, i.e.ARecord:fromnic_api.modelsimportARecordrecord_www=ARecord(name="www",a="8.8.8.8",ttl=3600)Add this record to the zone and commit the changes:api.add_record(record_www,"MY_SERVICE","example.com")api.commit("MY_SERVICE","example.com")Deleting a recordEvery record in the zone has an unique ID, and it's accessible viaDNSRecord.idproperty. When you got the ID, pass it to theDnsApi.delete_record()method:api.delete_record(100000,"MY_SERVICE","example.com")api.commit("MY_SERVICE","example.com")Do not forget to commit the changes afterwards.Default service and zoneTheserviceandzoneparameters can be optional in allDnsApimethods, if you setdefault_serviceanddefault_zoneproperties:api.default_service="MY_SERVICE"api.default_zone="example.com"api.delete_record(100000)# service or zone are not required nowapi.commit()# the same for commit() methodInternationalized domain namesFor using IDNs, you need to encode parameters with Punycode before passing them
to the classes or methods, with the only exception ofidn_nameargument. In
Python 3.7+ this is done via calling.encode("idna").decode()methods on thestrobject:record_idn=ARecord(a="192.168.0.1",name="тест".encode("idna").decode())api.add_record(record_idn,"MY_SERVICE","example.com")api.commit("MY_SERVICE","example.com")For working with top-level IDNs, use the same approach:api.records("MY_SERIVCE","мой-домен.рф".encode("idna").decode())# or:api.default_service="MY_SERVICE"api.default_zone="мой-домен.рф".encode("idna").decode()api.records()Note for Python old versions' usersPython 2.7 was EOLed on 2020-01-01, and Python 3.7 is going to be EOLed on
2023-06-27, so the 1.x.x release branch ofnic-apiis going to only support
active Python releases. If you still need old Python version support, please
use 0.x.x releases ofnic-api. These versions would still receive bugfixes,
but no enhancements.