Download Code Sample

This simple C# code sample uses only .NET libraries (—no external dependencies) to:

  • Authenticate to a controller
  • Read a point
  • Return the response JSON as a string

The sample calls the GetAnalogInputs() endpoint at an Opto 22 demo controller.

It was written with Visual Studio® 2013, using .NET 4.5.

//This C# example uses the SNAP PAC REST API to get the analog inputs and values
//from the specified controller. The example uses standard .NET libraries. 
//It doesn't use any external, third-party plug-ins. This code sample was 
//written with Visual Studio 2013, using .NET 4.5, running on 
//Windows 10 Anniversary Edition.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net; //If necessary, add this reference to your project.  
                  //You need it to authenticate to the API.
namespace GetAnalogInputs
    class Program
        static void Main(string[] args)
    public static class OptoApiClient
        //The class has one method. The method performs an HTTP GET request on 
        //the specified controller, passes in logon credentials, and calls the
        //GetAnalogInputs endpoint. 
        public static void GetAnalogInputs()
            //The controller is specified by its IP address or DNS server name.
            //This address hits a demo server. 
            //TO DO: Replace with your controller's IP 
            //address or server name. 
            var url =  "";
            //There are several ways to consume the API. We're using the 
            //WebClient class because it's simple and straightforward. 
            //Note that this class consumes the API synchronously. 
            var client = new WebClient
                    //TO DO: If you’ve changed the URL to access your 
                    //controller, then replace ro/ro with the key-value 
                    //configured for your credentials.
                    Credentials = new NetworkCredential("ro", "ro"),
            //This statement instantiates a string for the returned JSON.
            var content = client.DownloadString(url);
            Console.WriteLine("The returned JSON string is:");
            //This statement writes the JSON to the console.
            Console.WriteLine("Press Enter to close the console.");