Class ZonesApi
- Namespace
- Cloudflare.NET.Zones
- Assembly
- Cloudflare.NET.dll
Implements the API for managing Cloudflare Zone resources.
public class ZonesApi : ApiResource, IZonesApi
- Inheritance
-
ZonesApi
- Implements
- Inherited Members
Constructors
ZonesApi(HttpClient, ILoggerFactory)
Initializes a new instance of the ZonesApi class.
public ZonesApi(HttpClient httpClient, ILoggerFactory loggerFactory)
Parameters
httpClientHttpClientThe HttpClient for making requests.
loggerFactoryILoggerFactoryThe factory to create loggers for this and child resources.
Properties
AccessRules
Gets the API for managing zone-level IP Access Rules.
public IZoneAccessRulesApi AccessRules { get; }
Property Value
Remarks
Corresponds to the /zones/{zone_id}/firewall/access_rules/rules endpoint.
CustomHostnames
Gets the API for managing Custom Hostnames (Cloudflare for SaaS).
public ICustomHostnamesApi CustomHostnames { get; }
Property Value
Remarks
Corresponds to the /zones/{zone_id}/custom_hostnames endpoint family.
Lockdown
Gets the API for managing Zone Lockdown rules.
public IZoneLockdownApi Lockdown { get; }
Property Value
Remarks
Corresponds to the /zones/{zone_id}/firewall/lockdowns endpoint.
Rulesets
Gets the API for managing zone-level Rulesets (e.g., WAF, Redirects).
public IZoneRulesetsApi Rulesets { get; }
Property Value
Remarks
Corresponds to the /zones/{zone_id}/rulesets endpoint family.
UaRules
Gets the API for managing User-Agent blocking rules.
public IZoneUaRulesApi UaRules { get; }
Property Value
Remarks
Corresponds to the /zones/{zone_id}/firewall/ua_rules endpoint.
Methods
CreateCnameRecordAsync(string, string, string, CancellationToken)
Creates a CNAME DNS record, typically used to point a custom domain to Cloudflare's infrastructure.
public Task<DnsRecord> CreateCnameRecordAsync(string zoneId, string hostname, string cnameTarget, CancellationToken cancellationToken = default)
Parameters
zoneIdstringThe ID of the zone where the record will be created.
hostnamestringThe hostname for the CNAME record (e.g., "cdn.tenant.example.com").
cnameTargetstringThe target the CNAME should point to.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<DnsRecord>
A task that represents the asynchronous operation. The task result contains the DnsRecord with details of the created record.
CreateZoneAsync(CreateZoneRequest, CancellationToken)
Creates a new zone.
public Task<Zone> CreateZoneAsync(CreateZoneRequest request, CancellationToken cancellationToken = default)
Parameters
requestCreateZoneRequestThe zone creation request with name, type, and account.
cancellationTokenCancellationTokenA cancellation token.
Returns
Examples
var request = new CreateZoneRequest(
Name: "example.com",
Type: ZoneType.Full,
Account: new ZoneAccountReference("account-id")
);
var zone = await zonesApi.CreateZoneAsync(request);
Remarks
Preview: This operation has limited test coverage.
The zone will initially have a status of Pending until nameserver verification completes.
Use TriggerActivationCheckAsync(string, CancellationToken) to manually trigger verification.
Exceptions
- ArgumentNullException
Thrown when
requestis null.
- See Also
CreateZoneHoldAsync(string, bool, CancellationToken)
Creates/enforces a zone hold, blocking creation of zones with this hostname.
public Task<ZoneHold> CreateZoneHoldAsync(string zoneId, bool includeSubdomains = false, CancellationToken cancellationToken = default)
Parameters
zoneIdstringThe zone identifier.
includeSubdomainsboolWhen true, the hold extends to block any subdomain and SSL4SaaS Custom Hostnames. For example, a hold on "example.com" would also block "staging.example.com".
cancellationTokenCancellationTokenA cancellation token.
Returns
Examples
// Create a basic hold
var hold = await zonesApi.CreateZoneHoldAsync(zoneId);
// Create a hold that includes subdomains
var holdWithSubdomains = await zonesApi.CreateZoneHoldAsync(zoneId, includeSubdomains: true);
Remarks
Preview: This operation has limited test coverage.
Exceptions
- ArgumentNullException
Thrown when
zoneIdis null.- ArgumentException
Thrown when
zoneIdis empty or whitespace.
- See Also
DeleteDnsRecordAsync(string, string, CancellationToken)
Deletes a DNS record by its ID within a specific zone.
public Task DeleteDnsRecordAsync(string zoneId, string dnsRecordId, CancellationToken cancellationToken = default)
Parameters
zoneIdstringThe ID of the zone where the record exists.
dnsRecordIdstringThe unique identifier of the DNS record to delete.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task
A task that represents the asynchronous operation.
DeleteZoneAsync(string, CancellationToken)
Deletes a zone permanently.
public Task DeleteZoneAsync(string zoneId, CancellationToken cancellationToken = default)
Parameters
zoneIdstringThe identifier of the zone to delete.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task
A task that represents the asynchronous operation.
Remarks
Preview: This operation has limited test coverage.
Warning: This operation is irreversible. All DNS records, settings, and configuration for the zone will be permanently deleted.
Exceptions
- ArgumentNullException
Thrown when
zoneIdis null.- ArgumentException
Thrown when
zoneIdis empty or whitespace.
- See Also
EditZoneAsync(string, EditZoneRequest, CancellationToken)
Edits a zone's properties. Only one property can be changed per call.
public Task<Zone> EditZoneAsync(string zoneId, EditZoneRequest request, CancellationToken cancellationToken = default)
Parameters
zoneIdstringThe identifier of the zone to edit.
requestEditZoneRequestThe edit request with the property to change.
cancellationTokenCancellationTokenA cancellation token.
Returns
Examples
// Pause a zone
var request = new EditZoneRequest(Paused: true);
var zone = await zonesApi.EditZoneAsync(zoneId, request);
Remarks
Preview: This operation has limited test coverage.
Important: Only one property can be changed per API call. Use the convenience methods (SetZonePausedAsync(string, bool, CancellationToken), SetZoneTypeAsync(string, ZoneType, CancellationToken), SetVanityNameServersAsync(string, IReadOnlyList<string>, CancellationToken)) for clearer intent.
Exceptions
- ArgumentNullException
Thrown when
zoneIdorrequestis null.- ArgumentException
Thrown when
zoneIdis empty or whitespace.
- See Also
ExportDnsRecordsAsync(string, CancellationToken)
Exports all DNS records for a zone in BIND format.
public Task<string> ExportDnsRecordsAsync(string zoneId, CancellationToken cancellationToken = default)
Parameters
zoneIdstringThe ID of the zone.
cancellationTokenCancellationTokenA cancellation token.
Returns
FindDnsRecordByNameAsync(string, string, CancellationToken)
Finds a DNS record by its fully qualified name within a specific zone.
public Task<DnsRecord?> FindDnsRecordByNameAsync(string zoneId, string hostname, CancellationToken cancellationToken = default)
Parameters
zoneIdstringThe ID of the zone to search in.
hostnamestringThe name of the DNS record to find (e.g., "test.example.com").
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<DnsRecord>
A task that represents the asynchronous operation. The task result contains the DnsRecord if found; otherwise, null. If multiple records exist with the same name (e.g., A and AAAA), this method returns the first one from the API response.
GetZoneDetailsAsync(string, CancellationToken)
Fetches the details for a specific Zone by its ID.
public Task<Zone> GetZoneDetailsAsync(string zoneId, CancellationToken cancellationToken = default)
Parameters
zoneIdstringThe identifier of the Zone.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<Zone>
A task that represents the asynchronous operation. The task result contains the Zone details.
Exceptions
- ArgumentNullException
Thrown when
zoneIdis null.- ArgumentException
Thrown when
zoneIdis empty or whitespace.
- See Also
GetZoneHoldAsync(string, CancellationToken)
Gets the zone hold status and configuration.
public Task<ZoneHold> GetZoneHoldAsync(string zoneId, CancellationToken cancellationToken = default)
Parameters
zoneIdstringThe zone identifier.
cancellationTokenCancellationTokenA cancellation token.
Returns
Examples
var hold = await zonesApi.GetZoneHoldAsync(zoneId);
if (hold.Hold)
{
Console.WriteLine($"Zone is held since {hold.HoldAfter}");
}
Remarks
A zone hold prevents creation and activation of zones with the same hostname. Use CreateZoneHoldAsync(string, bool, CancellationToken) to create a hold and RemoveZoneHoldAsync(string, CancellationToken) to remove it.
Exceptions
- ArgumentNullException
Thrown when
zoneIdis null.- ArgumentException
Thrown when
zoneIdis empty or whitespace.
- See Also
GetZoneSettingAsync(string, ZoneSettingId, CancellationToken)
Gets a single zone setting by its identifier.
public Task<ZoneSetting> GetZoneSettingAsync(string zoneId, ZoneSettingId settingId, CancellationToken cancellationToken = default)
Parameters
zoneIdstringThe zone identifier.
settingIdZoneSettingIdThe setting identifier. Use ZoneSettingId static properties for known settings.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<ZoneSetting>
The zone setting with its current value.
Examples
// Using the extensible enum (recommended)
var setting = await zones.GetZoneSettingAsync(zoneId, ZoneSettingId.MinTlsVersion);
string version = setting.Value.GetString(); // "1.2"
var cacheTtl = await zones.GetZoneSettingAsync(zoneId, ZoneSettingId.BrowserCacheTtl);
int ttlSeconds = cacheTtl.Value.GetInt32(); // 14400
// String values are implicitly converted to ZoneSettingId
var custom = await zones.GetZoneSettingAsync(zoneId, "custom_setting");
Exceptions
- ArgumentNullException
Thrown when
zoneIdis null.- ArgumentException
Thrown when
zoneIdis empty or whitespace.
- See Also
ImportDnsRecordsAsync(string, Stream, bool, bool, CancellationToken)
Imports a BIND file to bulk create/overwrite DNS records.
public Task<DnsImportResult> ImportDnsRecordsAsync(string zoneId, Stream bindStream, bool proxied, bool overwriteExisting, CancellationToken cancellationToken = default)
Parameters
zoneIdstringThe ID of the zone.
bindStreamStreamA stream containing the BIND configuration file.
proxiedboolWhether to proxy the imported records.
overwriteExistingboolWhether to overwrite existing records.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<DnsImportResult>
A result object with a summary of the import operation.
ListAllDnsRecordsAsync(string, ListDnsRecordsFilters?, CancellationToken)
Lists all DNS records for a zone, automatically handling pagination.
public IAsyncEnumerable<DnsRecord> ListAllDnsRecordsAsync(string zoneId, ListDnsRecordsFilters? filters = null, CancellationToken cancellationToken = default)
Parameters
zoneIdstringThe ID of the zone.
filtersListDnsRecordsFiltersOptional filters for sorting and matching. Pagination options will be ignored.
cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<DnsRecord>
An asynchronous stream of all matching DNS records.
ListAllZonesAsync(ListZonesFilters?, CancellationToken)
Lists all zones, automatically handling pagination.
public IAsyncEnumerable<Zone> ListAllZonesAsync(ListZonesFilters? filters = null, CancellationToken cancellationToken = default)
Parameters
filtersListZonesFiltersOptional filters for sorting and matching. Pagination options will be ignored.
cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<Zone>
An asynchronous stream of all matching zones.
Examples
await foreach (var zone in zonesApi.ListAllZonesAsync())
{
Console.WriteLine($"{zone.Name}: {zone.Status}");
}
Remarks
This method automatically handles pagination by making multiple API requests as needed. The Page and PerPage properties are managed internally and will be ignored if provided.
- See Also
ListDnsRecordsAsync(string, ListDnsRecordsFilters?, CancellationToken)
Lists DNS records for a zone, with filtering and manual pagination.
public Task<PagePaginatedResult<DnsRecord>> ListDnsRecordsAsync(string zoneId, ListDnsRecordsFilters? filters = null, CancellationToken cancellationToken = default)
Parameters
zoneIdstringThe ID of the zone.
filtersListDnsRecordsFiltersOptional filters for pagination, sorting, and matching.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<PagePaginatedResult<DnsRecord>>
A single page of DNS records along with pagination information.
ListZonesAsync(ListZonesFilters?, CancellationToken)
Lists zones with filtering and pagination.
public Task<PagePaginatedResult<Zone>> ListZonesAsync(ListZonesFilters? filters = null, CancellationToken cancellationToken = default)
Parameters
filtersListZonesFiltersOptional filters for pagination, sorting, and matching.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<PagePaginatedResult<Zone>>
A single page of zones along with pagination information.
Examples
// List active zones
var result = await zonesApi.ListZonesAsync(new ListZonesFilters(Status: ZoneStatus.Active));
foreach (var zone in result.Items)
{
Console.WriteLine($"{zone.Name}: {zone.Status}");
}
- See Also
PurgeCacheAsync(string, PurgeCacheRequest, CancellationToken)
Purges assets from the Cloudflare cache for a zone.
public Task<PurgeCacheResult> PurgeCacheAsync(string zoneId, PurgeCacheRequest request, CancellationToken cancellationToken = default)
Parameters
zoneIdstringThe ID of the zone.
requestPurgeCacheRequestThe request defining what to purge (e.g., files, prefixes, or everything).
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<PurgeCacheResult>
The result of the purge operation.
RemoveZoneHoldAsync(string, CancellationToken)
Removes a zone hold, allowing creation of zones with this hostname.
public Task<ZoneHold> RemoveZoneHoldAsync(string zoneId, CancellationToken cancellationToken = default)
Parameters
zoneIdstringThe zone identifier.
cancellationTokenCancellationTokenA cancellation token.
Returns
Examples
var result = await zonesApi.RemoveZoneHoldAsync(zoneId);
// result.Hold will be false
Exceptions
- ArgumentNullException
Thrown when
zoneIdis null.- ArgumentException
Thrown when
zoneIdis empty or whitespace.
- See Also
SetVanityNameServersAsync(string, IReadOnlyList<string>, CancellationToken)
Sets the zone's vanity nameservers.
public Task<Zone> SetVanityNameServersAsync(string zoneId, IReadOnlyList<string> nameservers, CancellationToken cancellationToken = default)
Parameters
zoneIdstringThe identifier of the zone.
nameserversIReadOnlyList<string>The list of vanity nameservers to set.
cancellationTokenCancellationTokenA cancellation token.
Returns
Remarks
Vanity nameservers require a Business or Enterprise plan. These are custom-branded nameservers (e.g., ns1.yourdomain.com instead of ns1.cloudflare.com).
Exceptions
- ArgumentNullException
Thrown when
zoneIdornameserversis null.- ArgumentException
Thrown when
zoneIdis empty or whitespace.
SetZonePausedAsync(string, bool, CancellationToken)
Sets the zone's paused state.
public Task<Zone> SetZonePausedAsync(string zoneId, bool paused, CancellationToken cancellationToken = default)
Parameters
zoneIdstringThe identifier of the zone.
pausedboolWhether to pause the zone.
cancellationTokenCancellationTokenA cancellation token.
Returns
Examples
// Pause traffic proxying
var zone = await zonesApi.SetZonePausedAsync(zoneId, true);
// Resume traffic proxying
zone = await zonesApi.SetZonePausedAsync(zoneId, false);
Remarks
When a zone is paused, Cloudflare stops proxying traffic and the zone essentially becomes DNS-only.
Exceptions
- ArgumentNullException
Thrown when
zoneIdis null.- ArgumentException
Thrown when
zoneIdis empty or whitespace.
SetZoneSettingAsync<T>(string, ZoneSettingId, T, CancellationToken)
Updates a zone setting.
public Task<ZoneSetting> SetZoneSettingAsync<T>(string zoneId, ZoneSettingId settingId, T value, CancellationToken cancellationToken = default)
Parameters
zoneIdstringThe zone identifier.
settingIdZoneSettingIdThe setting identifier. Use ZoneSettingId static properties for known settings.
valueTThe new value for the setting.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<ZoneSetting>
The updated zone setting.
Type Parameters
TThe value type (string, int, or complex object).
Examples
// Using the extensible enum (recommended)
await zones.SetZoneSettingAsync(zoneId, ZoneSettingId.MinTlsVersion, "1.2");
// Enable development mode
await zones.SetZoneSettingAsync(zoneId, ZoneSettingId.DevelopmentMode, "on");
// Set browser cache TTL to 4 hours
await zones.SetZoneSettingAsync(zoneId, ZoneSettingId.BrowserCacheTtl, 14400);
// String values are implicitly converted to ZoneSettingId
await zones.SetZoneSettingAsync(zoneId, "custom_setting", "value");
Exceptions
- ArgumentNullException
Thrown when
zoneIdis null.- ArgumentException
Thrown when
zoneIdis empty or whitespace.
- See Also
SetZoneTypeAsync(string, ZoneType, CancellationToken)
Sets the zone's type.
public Task<Zone> SetZoneTypeAsync(string zoneId, ZoneType type, CancellationToken cancellationToken = default)
Parameters
zoneIdstringThe identifier of the zone.
typeZoneTypeThe zone type to set.
cancellationTokenCancellationTokenA cancellation token.
Returns
Remarks
Changing zone type may require Enterprise plan for certain transitions. Not all transitions are supported (e.g., secondary to full may not be available).
Exceptions
- ArgumentNullException
Thrown when
zoneIdis null.- ArgumentException
Thrown when
zoneIdis empty or whitespace.
TriggerActivationCheckAsync(string, CancellationToken)
Triggers activation check for a pending zone.
public Task<ActivationCheckResult> TriggerActivationCheckAsync(string zoneId, CancellationToken cancellationToken = default)
Parameters
zoneIdstringThe identifier of the zone to check.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<ActivationCheckResult>
The result containing the zone ID.
Remarks
Rate limited: every 5 minutes (paid plans), every hour (free plans). The API only returns the zone ID; use GetZoneDetailsAsync(string, CancellationToken) to fetch updated status.
Exceptions
- ArgumentNullException
Thrown when
zoneIdis null.- ArgumentException
Thrown when
zoneIdis empty or whitespace.
- See Also
UpdateZoneHoldAsync(string, UpdateZoneHoldRequest, CancellationToken)
Updates an existing zone hold's configuration.
public Task<ZoneHold> UpdateZoneHoldAsync(string zoneId, UpdateZoneHoldRequest request, CancellationToken cancellationToken = default)
Parameters
zoneIdstringThe zone identifier.
requestUpdateZoneHoldRequestThe update parameters.
cancellationTokenCancellationTokenA cancellation token.
Returns
Examples
// Schedule a hold for the future
var request = new UpdateZoneHoldRequest(HoldAfter: DateTime.UtcNow.AddDays(7));
var hold = await zonesApi.UpdateZoneHoldAsync(zoneId, request);
// Enable subdomain protection
var request2 = new UpdateZoneHoldRequest(IncludeSubdomains: true);
var hold2 = await zonesApi.UpdateZoneHoldAsync(zoneId, request2);
Remarks
Preview: This operation has limited test coverage.
Both HoldAfter and IncludeSubdomains are optional. Only the provided fields will be updated.
Exceptions
- ArgumentNullException
Thrown when
zoneIdorrequestis null.- ArgumentException
Thrown when
zoneIdis empty or whitespace.
- See Also