Wiki

Clone wiki

CloudFlare.API / Home

CloudFlare API C# Library

Welcome to the home for CloudFlare API Library. With this C# library, you can add programatically access to your CloudFlare account and list the domains, get some statistics or manage the DNS Records from within your .Net app.

You NEED to have a CloudFlare account to access the API. You can find your API Key at your account page: https://www.cloudflare.com/my-account. You will need your Api key and your email later to config the library.

Release notes

You can find the release notes here.

Installation

Just use Nuget (https://www.nuget.org/packages/CloudFlare.API/):

  • Search for "CloudFlare.API" with the Library Package Manager in VS.
  • Or type in the Package Manager Console

    Install-Package CloudFlare.API

This will install the reference to the library in your project.

The next step is to config the library. To do so, you'll need to open your app.config or web.config file, and look for the CloudFlareAPI configuration section, which looks like this, and replace the placeholder values with your Api key and email at CloudFlare:

#!xml
  <CloudFlareAPI>
    <add key="Enabled" value="true" />
    <add key="BaseUrl" value="https://www.cloudflare.com/api_json.html" />
    <add key="ApiKey" value="[YOUR API KEY]" />
    <add key="Email" value="[YOUR EMAIL]" />
  </CloudFlareAPI>

Features supported

You can find the full features offered by the CloudFlare API at http://www.cloudflare.com/docs/client-api.html.

At this moment, the CloudFlare.API library supports the current features

Playing with it

Here is a small example that makes some calls to the API. Put it on a small Console project:

#!c#
using CloudFlare.API;
using CloudFlare.API.Enums;

try
{
    // RETRIEVE COMPLETE DOMAINS LIST
    System.Console.WriteLine("DOMAINS LIST\n----------------------------------------------");
    var domains = CFProxy.Access.GetDomains();
    foreach (var zone in domains.objs)
    {
        System.Console.WriteLine("{0} - {1} ({2})", zone.zone_id, zone.zone_name, zone.display_name);
    }
    System.Console.WriteLine("");

    if (domains.count > 0)
    {
        // RETRIEVE FIRST DOMAIN STATISTICS
        string domain = domains.objs[0].zone_name;
        System.Console.WriteLine("STATS FOR {0}\n----------------------------------------------", domain);
        var stats = CFProxy.Access.GetStats(domain, Intervals.Days30);
        if (stats.count > 0)
        {
            System.Console.WriteLine("PageViews: {0}",  stats.objs[0].trafficBreakdown.pageviews.crawler +
                                                        stats.objs[0].trafficBreakdown.pageviews.regular +
                                                        stats.objs[0].trafficBreakdown.pageviews.threat);
            System.Console.WriteLine("Visits: {0}", stats.objs[0].trafficBreakdown.uniques.crawler +
                                                    stats.objs[0].trafficBreakdown.uniques.regular +
                                                    stats.objs[0].trafficBreakdown.uniques.threat);
        }
        System.Console.WriteLine("");

        // RETRIEVE COMPLETE DNS RECORDS LIST FOR FIRST DOMAIN
        System.Console.WriteLine("DNS RECORDS FOR {0}\n----------------------------------------------", domain);
        var records = CFProxy.Access.GetRecords(domain);
        foreach (var dnsObject in records)
        {
            System.Console.WriteLine("{0}\t{1}\t -> {2}", dnsObject.type, dnsObject.name, dnsObject.content);
        }
    }
}
catch (ApplicationException ex)
{
    var error = String.Format("ERROR: {0}", ex.Message);
    if (ex.InnerException != null)
        error += String.Format("\nInner exception: {0}", ex.InnerException.Message);

    System.Console.WriteLine(error);
}

Note about error handling

If there is a network transport error (network is down, failed DNS lookup, etc), CFProxy will throw an ApplicationException with the details and the InnerException if available.

If the error comes from the API (e.g. querying for a domain not in our account), CFProxy will throw the ApplicationException as well, with the error message received from the API in the Exception.Message property.

So, always surround the API calls with try/catch to prevent errors.

Disclaimer

This is a first alpha release, so it's not as stable as it could be, but enough to play around with it. The source code will be open sourced soon so that you could contribute and improve it if you wish ;) Use it at your own risk.

I am not related with CloudFlare in any way. This is not an official CloudFlare library. It's just a small piece of my work :)

The license applied for using this software is the MIT License.

Updated