Skip to content

C# Client

Akram El Assas edited this page Jul 16, 2025 · 4 revisions

Here is a sample C# client:

using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Wexflow.Core.Service.Contracts;

namespace Wexflow.Core.Service.Client
{
    public class WexflowServiceClient(string uri)
    {
        public string Uri { get; } = uri.TrimEnd('/');

        private static async Task<string> DownloadStringAsync(HttpClient client, string url, string token)
        {
            HttpRequestMessage request = new(HttpMethod.Get, url);
            if (!string.IsNullOrEmpty(token))
            {
                request.Headers.Add("Authorization", $"Bearer {token}");
            }
            var response = await client.SendAsync(request);
            var byteArray = await response.Content.ReadAsByteArrayAsync();
            var responseString = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);
            return responseString;
        }

        private static async Task<string> UploadStringAsync(HttpClient client, string url, string token, string body = "")
        {
            HttpRequestMessage request = new(HttpMethod.Post, url);
            if (!string.IsNullOrEmpty(token))
            {
                request.Headers.Add("Authorization", $"Bearer {token}");
            }
            if (!string.IsNullOrEmpty(body))
            {
                request.Content = new StringContent(body, Encoding.UTF8, "application/json");
            }
            var response = await client.SendAsync(request);
            var byteArray = await response.Content.ReadAsByteArrayAsync();
            var responseString = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);
            return responseString;
        }

        public async Task<string> Login(string username, string password)
        {
            var uri = $"{Uri}/login";
            using HttpClient webClient = new();

            var requestBody = JsonConvert.SerializeObject(new { username, password });
            var response = await UploadStringAsync(webClient, uri, null, requestBody);

            // Deserialize response JSON into a dynamic object
            dynamic res = JsonConvert.DeserializeObject(response);

            // Return the access_token property
            return res?.access_token;
        }

        public async Task<WorkflowInfo[]> Search(string keyword, string token)
        {
            var uri = $"{Uri}/search?s={keyword}";
            using HttpClient webClient = new();

            var response = await DownloadStringAsync(webClient, uri, token);
            var workflows = JsonConvert.DeserializeObject<WorkflowInfo[]>(response);
            return workflows;
        }

        public async Task<Guid> StartWorkflow(int id, string token)
        {
            var uri = $"{Uri}/start?w={id}";
            using HttpClient webClient = new();
            var instanceId = await UploadStringAsync(webClient, uri, token);
            return Guid.Parse(instanceId.Replace("\"", string.Empty));
        }

        public async Task<Guid> StartWorkflowWithVariables(string payload, string token)
        {
            var uri = $"{Uri}/start-with-variables";
            using HttpClient webClient = new();
            var instanceId = await UploadStringAsync(webClient, uri, token, payload);
            return Guid.Parse(instanceId.Replace("\"", string.Empty));
        }

        public async Task StopWorkflow(int id, Guid instanceId, string token)
        {
            var uri = $"{Uri}/stop?w={id}&i={instanceId}";
            using HttpClient webClient = new();
            _ = await UploadStringAsync(webClient, uri, token);
        }

        public async Task SuspendWorkflow(int id, Guid instanceId, string token)
        {
            var uri = $"{Uri}/suspend?w={id}&i={instanceId}";
            using HttpClient webClient = new();
            _ = await UploadStringAsync(webClient, uri, token);
        }

        public async Task ResumeWorkflow(int id, Guid instanceId, string token)
        {
            var uri = $"{Uri}/resume?w={id}&i={instanceId}";
            using HttpClient webClient = new();
            _ = await UploadStringAsync(webClient, uri, token);
        }

        public async Task ApproveWorkflow(int id, Guid instanceId, string token)
        {
            var uri = $"{Uri}/approve?w={id}&i={instanceId}";
            using HttpClient webClient = new();
            _ = await UploadStringAsync(webClient, uri, token);
        }

        public async Task RejectWorkflow(int id, Guid instanceId, string token)
        {
            var uri = $"{Uri}/reject?w={id}&i={instanceId}";
            using HttpClient webClient = new();
            _ = await UploadStringAsync(webClient, uri, token);
        }

        public async Task<WorkflowInfo> GetWorkflow(string token, int id)
        {
            var uri = $"{Uri}/workflow?w={id}";
            using HttpClient webClient = new();
            var response = await DownloadStringAsync(webClient, uri, token);
            var workflow = JsonConvert.DeserializeObject<WorkflowInfo>(response);
            return workflow;
        }

        public async Task<User> GetUser(string username, string token)
        {
            var uri = $"{Uri}/user?username={System.Uri.EscapeDataString(username)}";
            using HttpClient webClient = new();
            var response = await DownloadStringAsync(webClient, uri, token);
            var user = JsonConvert.DeserializeObject<User>(response);
            return user;
        }
    }
}
  1. Install Guide
  2. Migration Guide to v10.0
  3. HTTPS/SSL
  4. Screenshots
  5. Docker
  6. Configuration Guide
    1. Wexflow Server
    2. Wexflow.xml
    3. Admin Panel
    4. Authentication
  7. Persistence Providers
  8. Getting Started
  9. Android App
  10. Local Variables
  11. Global Variables
  12. REST Variables
  13. Functions
  14. Cron Scheduling
  15. Command Line Interface (CLI)
  16. REST API Reference
    1. Introduction
    2. Authentication
    3. Sample Clients
      1. C# Client
      2. PowerShell Client
      3. JavaScript Client
      4. PHP Client
      5. Python Client
      6. Go Client
      7. Rust Client
      8. Ruby Client
      9. Java Client
      10. C++ Client
    4. Security Considerations
    5. Swagger
    6. Workflow Notifications via SSE
      1. C# SSE Client
      2. PowerShell SSE Client
      3. JavaScript SSE Client
      4. PHP SSE Client
      5. Python SSE Client
      6. Go SSE Client
      7. Rust SSE Client
      8. Ruby SSE Client
      9. Java SSE Client
      10. C++ SSE Client
    7. Endpoints
  17. Samples
    1. Sequential workflows
    2. Execution graph
    3. Flowchart workflows
      1. If
      2. While
      3. Switch
    4. Approval workflows
      1. Simple approval workflow
      2. OnRejected workflow event
      3. YouTube approval workflow
      4. Form submission approval workflow
    5. Workflow events
  18. Logging
  19. Custom Tasks
    1. Introduction
    2. General
      1. Creating a Custom Task
      2. Wexflow Task Class Example
      3. Task Status
      4. Settings
      5. Loading Files
      6. Loading Entities
      7. Need A Starting Point?
    3. Installing Your Custom Task in Wexflow
      1. .NET Framework 4.8 (Legacy Version)
      2. .NET 8.0+ (Stable Version)
      3. Referenced Assemblies
      4. Updating a Custom Task
      5. Using Your Custom Task
    4. Suspend/Resume
    5. Logging
    6. Files
    7. Entities
    8. Shared Memory
    9. Designer Integration
      1. Registering the Task
      2. Adding Settings
    10. How to Debug a Custom Task?
  20. Built-in Tasks
    1. File system tasks
    2. Encryption tasks
    3. Compression tasks
    4. Iso tasks
    5. Speech tasks
    6. Hashing tasks
    7. Process tasks
    8. Network tasks
    9. XML tasks
    10. SQL tasks
    11. WMI tasks
    12. Image tasks
    13. Audio and video tasks
    14. Email tasks
    15. Workflow tasks
    16. Social media tasks
    17. Waitable tasks
    18. Reporting tasks
    19. Web tasks
    20. Script tasks
    21. JSON and YAML tasks
    22. Entities tasks
    23. Flowchart tasks
    24. Approval tasks
    25. Notification tasks
    26. SMS tasks
  21. Run from Source
  22. Fork, Customize, and Sync

Clone this wiki locally