C# Example

SAPI endpoints can easily be called from a .NET application. The following example is one way of invoking SAPI using C# and the standard .NET library. This can be used as a basis for building a .NET assembly for use in other .NET languages, such as Visual Basic.


In order for this example to work, you will need:

  • .NET Framework 3.5 or equivalent Mono runtime
  • Direct access to the Internet (ie, no proxy)

Tip: It is possible to access SAPI through an HTTP proxy, but it is not covered here. See the System.Net.WebRequest documentation for more details.

Basic Example

The following example calls the Search endpoint and prints the name and address details for each listing returned in the first page of results.


using System;
using System.Net;
using System.Runtime.Serialization.Json;

namespace SsapiExample
    class MainClass
        public static void Main(string[] args)
            var searchEndPoint = "http://api.sensis.com.au/v1/test/search";
            var apiKey = "your-api-key";

            var searcher = new SsapiSearcher(searchEndPoint, apiKey);

            // Perform a search and check the response
            var searchResponse = searcher.SearchFor("cafe", "melbourne");
            if (searchResponse.code < 200 || searchResponse.code > 299) {
                Console.WriteLine("Search failed - Error " + searchResponse.code + ": " + searchResponse.message);

            Console.WriteLine("Total results found: " + searchResponse.totalResults);

            // Display the results
            foreach (var result in searchResponse.results) {
                Print(result.primaryAddress.suburb + " " + result.primaryAddress.state + " " + result.primaryAddress.postcode);
                Print(result.primaryAddress.latitude + ", " + result.primaryAddress.longitude);

        static void Print(object value)
            // Avoid printing nulls and blanks
            if (value != null && value.ToString().Trim() != "")

    class SsapiSearcher
        readonly Uri endPoint;
        readonly string apiKey;

        public SsapiSearcher(string endPoint, string apiKey)
            this.endPoint = new Uri(endPoint);
            this.apiKey = apiKey;

        public SearchResponse SearchFor(string query, string location)
            // Build the API request
            var url = new Uri(endPoint, "?query=" + Uri.EscapeDataString(query) + "&location=" + Uri.EscapeDataString(location) + "&key=" + Uri.EscapeDataString(apiKey));
            var req = WebRequest.Create(url);

            // Send the request and read the response
            using (var res = req.GetResponse()) {
                var serializer = new DataContractJsonSerializer(typeof(SearchResponse));
                return serializer.ReadObject(res.GetResponseStream()) as SearchResponse;

    class SearchResponse
        public int code;
        public string message;
        public int totalResults;
        public Listing[] results;

    class Listing
        public string name;
        public Address primaryAddress;

    class Address
        public string addressLine;
        public string suburb;
        public string state;
        public string postcode;
        public double latitude;
        public double longitude;

Calling Other Endpoints

Calling the other endpoints is also easy. This can be done in many ways, the simplest of which would be to adjust the value of the searchEndPoint variable passed to the SsapiSearcher.