Skip to main content

C# Code Examples

Complete C# examples for integrating with CCC APIs.

Prerequisites

  • .NET Framework 4.5+ or .NET Core 2.0+
  • System.Net.Http namespace
  • Newtonsoft.Json (optional, for JSON parsing)
Always use TLS 1.2 or above. Add this line before making API calls:
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

Scrub API - HTTP GET Example

using System;
using System.Net.Http;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        // Use TLS 1.2
        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

        using (var client = new HttpClient())
        {
            var loginId = "YOUR_API_KEY";
            var phoneNumbersToScrub = "5039367187,7075276405";
            var version = "5";
            var campaignId = "YOUR_CAMPAIGN_ID"; // Optional

            var scrubRequest = string.Format(
                "https://www.dncscrub.com/app/main/rpc/scrub?loginId={0}&phoneList={1}&version={2}&campaignId={3}",
                loginId, phoneNumbersToScrub, version, campaignId
            );

            var responseString = await client.GetStringAsync(scrubRequest);
            var response = new System.IO.StringReader(responseString);

            // Iterate through each line of the response (CSV format)
            while (true)
            {
                var eachPhoneRecord = response.ReadLine();
                if (eachPhoneRecord != null)
                {
                    var elements = eachPhoneRecord.Split(',');
                    var phoneNumber = elements[0];
                    var scrubCode = elements[1];

                    switch (scrubCode)
                    {
                        case "C":
                            Console.WriteLine($"{phoneNumber}: Clean - OK to call");
                            break;

                        case "W":
                        case "L":
                        case "G":
                        case "H":
                        case "F":
                            Console.WriteLine($"{phoneNumber}: Wireless number");
                            break;

                        case "D":
                            Console.WriteLine($"{phoneNumber}: Do Not Call");
                            break;

                        default:
                            Console.WriteLine($"{phoneNumber}: Result code {scrubCode}");
                            break;
                    }
                }
                else
                {
                    break;
                }
            }
        }
    }
}

Scrub API - HTTP POST Example

Use POST for more than 50 phone numbers:
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        // Use TLS 1.2
        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

        try
        {
            using (var client = new HttpClient())
            {
                var formParams = new List<KeyValuePair<string, string>>
                {
                    new KeyValuePair<string, string>("loginId", "YOUR_API_KEY"),
                    new KeyValuePair<string, string>("phoneList", "5039367187,7075276405,7072842774"),
                    new KeyValuePair<string, string>("version", "5"),
                    new KeyValuePair<string, string>("output", "json")
                };

                var formContent = new FormUrlEncodedContent(formParams);
                var response = await client.PostAsync(
                    "https://www.dncscrub.com/app/main/rpc/scrub",
                    formContent
                );

                var responseString = await response.Content.ReadAsStringAsync();
                Console.WriteLine("Response: " + responseString);
            }
        }
        catch (HttpRequestException e)
        {
            Console.WriteLine($"HTTP Error: {e.Message}");
        }
        catch (Exception e)
        {
            Console.WriteLine($"Error: {e.Message}");
        }
    }
}

Scrub API with Header Authentication

More secure - pass API key in header:
using System;
using System.Net.Http;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

        using (var client = new HttpClient())
        {
            // Add API key to header instead of query string
            client.DefaultRequestHeaders.Add("loginId", "YOUR_API_KEY");

            var url = "https://www.dncscrub.com/app/main/rpc/scrub?phoneList=7075276405&version=5&output=json";
            var response = await client.GetStringAsync(url);

            Console.WriteLine(response);
        }
    }
}

TCPA Authority API Example

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

class Program
{
    static async Task Main(string[] args)
    {
        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Add("loginId", "YOUR_API_KEY");

            // Single number check
            var url = "https://dataapi.dncscrub.com/v1.4/Data/TCPAAuthority?phoneNumber=7075276405&date=20210209";
            var response = await client.GetStringAsync(url);

            var result = JsonConvert.DeserializeObject<TCPAAuthorityResponse>(response);

            if (result.IsReassigned == true)
            {
                Console.WriteLine("DO NOT CALL - Number has been reassigned");
            }
            else if (result.IsReassigned == false)
            {
                Console.WriteLine("Safe to call");
            }
            else
            {
                Console.WriteLine("Unable to determine");
            }
        }
    }
}

public class TCPAAuthorityResponse
{
    public string PhoneNumber { get; set; }
    public bool? IsReassigned { get; set; }
    public bool IsValid { get; set; }
    public string LineType { get; set; }
    public string Carrier { get; set; }
    public string Locale { get; set; }
    public string Region { get; set; }
    public string Country { get; set; }
    public string TZ { get; set; }
    public string UTCOffset { get; set; }
}

Litigator API Example

using System;
using System.Net.Http;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Add("loginId", "YOUR_API_KEY");

            var url = "https://dataapi.dncscrub.com/v1.4/scrub/litigator?phoneList=5039367187,7075276405";
            var response = await client.GetStringAsync(url);

            Console.WriteLine(response);
            // [{"Phone":5039367187,"IsLitigator":true},{"Phone":7075276405,"IsLitigator":false}]
        }
    }
}

Internal DNC - Add Number

using System;
using System.Net.Http;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Add("loginId", "YOUR_API_KEY");

            var url = "https://www.dncscrub.com/app/main/rpc/pdnc?phoneList=5039367187&actionType=add";
            var response = await client.GetAsync(url);

            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("Phone number added to Internal DNC");
            }
            else
            {
                var error = await response.Content.ReadAsStringAsync();
                Console.WriteLine($"Error: {error}");
            }
        }
    }
}

Error Handling Best Practices

using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

class ApiClient
{
    private readonly HttpClient _client;

    public ApiClient(string apiKey)
    {
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

        _client = new HttpClient();
        _client.DefaultRequestHeaders.Add("loginId", apiKey);
        _client.Timeout = TimeSpan.FromSeconds(30);
    }

    public async Task<string> ScrubPhoneNumbersAsync(string[] phoneNumbers)
    {
        try
        {
            var phoneList = string.Join(",", phoneNumbers);
            var url = $"https://www.dncscrub.com/app/main/rpc/scrub?phoneList={phoneList}&version=5&output=json";

            var response = await _client.GetAsync(url);

            if (response.StatusCode == HttpStatusCode.Unauthorized)
            {
                throw new Exception("Invalid API Key");
            }

            if (response.StatusCode == (HttpStatusCode)429)
            {
                throw new Exception("Rate limit exceeded. Please slow down requests.");
            }

            response.EnsureSuccessStatusCode();
            return await response.Content.ReadAsStringAsync();
        }
        catch (HttpRequestException ex)
        {
            throw new Exception($"Network error: {ex.Message}", ex);
        }
        catch (TaskCanceledException)
        {
            throw new Exception("Request timed out");
        }
    }
}