Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

How to call the OpenShift REST API from C#

July 17, 2018
Takayoshi Tanaka
Related topics:
.NETKubernetes
Related products:
Red Hat OpenShift Container Platform

    When you want to do automated tasks for builds and deployments with Red Hat OpenShift, you might want to take advantage of the OpenShift REST API. In scripts you can use oc CLI command which talks to the REST APIs. However there are times when it is more convenient to do this directly from your C# code without having to invoke an external program. This is the value of having an infrastructure platform that is exposed as services with an open API.

    If you want to call the API from your C# code, you have to create a request object, call the API, and parse the response object. The upstream project, OpenShift Origin, provides a Swagger 2.0 specification and you can generate a client library for each programming language. Of course, C# is supported.  This isn't a new approach, Kubernetes has a repository that is generated by Swagger Codegen.

    For C#, we can use Microsoft Visual Studio to generate a C# client library for a REST API. In this article, I'll walk you through the process of generating the library from the definition.

    Visual Studio can accept the Swagger 2.0 definition directly, but the OpenShift Swagger 2.0 API definition has some issues generating a C# library. However, with Kubernetes, some conversions are defined ussing JSONPath, see the GitHub repo.

    Following these conversions, I wrote some C# code to apply the required conversions to the original definition. If you want to generate code for your own project, please feel free to use my project to convert a definition file. Then you can generate C# code from the generated definition file.

    Now, prepare the placeholder project in Visual Studio. The project framework should be .NET Framework instead of .NET Core. It seems this is just a limit of the tooling. You can create a .NET Core (netstandard or netcoreapp) client library later. After creating a .NET Framework project, right-click the REST API Client context menu in the project and select Add.

    Then specify the file path for the generated definition file.

    If you want to use the client library with .NET Core, please create a placeholder project. I'm using a .NET Core class library project. Then, modify the csproj file, as follows, to add a package reference for Microsoft.Rest.ClientRuntime.

    <Project Sdk="Microsoft.NET.Sdk">
    
       <PropertyGroup>
         <TargetFrameworks>netstandard2.0</TargetFrameworks>
       </PropertyGroup>
    
       <ItemGroup>
         <PackageReference Include="Microsoft.Rest.ClientRuntime" Version="2.3.11" />
       </ItemGroup>
    
    </Project>
    

    Then just simply copy and paste the folder including the generated code.

    I publish the NuGet package for this generated project. Since the project is not listed in the search results, please specify the full name OpenShift.Service.Core to add the package reference into your own C# project. You also have to add the Microsoft.Rest.ClientRuntime package reference.

    Now you can write C# code to call the OpenShift REST API. Here is a simple example to call a GET pod REST API.

    //if your master URL has an invalid SSL, please add a custom validation callback.
    var handler = new HttpClientHandler
    {
        ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
    };
    //replace the acutual OpenShift master host name and your token to log in.
    var client = new OpenShiftAPIwithKubernetes(new Uri("https://:8443/"), new TokenCredentials(""), handler);
    var pods = await client.ListCoreV1NamespacedPodAsync("");
    foreach (var pod in pods.Items)
    {
        Console.WriteLine(pod.Metadata.Name + "=" + pod.Status.Phase);
    }
    

    To get a token for test purposes, log in to the OpenShift web console and select Copy Login Command from the context menu in the upper right corner (to the right of the user icon).

    The pasted command has the following format. Use the master_host value and token value. Note that this token will expire. We recommend using service accounts for production purposes.

    oc login https://<master_host>:8443 --token=<token>
    

    Enjoy writing code to work with the OpenShift REST API. If you find any issues, please send feedback to my repository.

    Last updated: February 25, 2022

    Recent Posts

    • Debugging image mode with Red Hat OpenShift 4.20: A practical guide

    • EvalHub: Because "looks good to me" isn't a benchmark

    • SQL Server HA on RHEL: Meet Pacemaker HA Agent v2 (tech preview)

    • Deploy with confidence: Continuous integration and continuous delivery for agentic AI

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.