C#
A small client class for .NET 6 or later, using HttpClient. It signs each request with the API token and deserializes the JSON response.
The API must be enabled first, see Enabling the API. How a request is signed is explained in Authentication.
Client
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
/// <summary>
/// Calls the FileFlows API, each request is signed with the API token (the token is never sent)
/// </summary>
public class FileFlowsApi(string serverUrl, string apiToken)
{
private readonly HttpClient _client = new() { BaseAddress = new Uri(serverUrl) };
public async Task<T?> SendAsync<T>(HttpMethod method, string path, object? body = null)
{
string json = body == null ? string.Empty : JsonSerializer.Serialize(body);
using var request = new HttpRequestMessage(method, path);
if (body != null)
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
string timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString();
string nonce = Guid.NewGuid().ToString("N");
string bodyHash = json.Length == 0 ? string.Empty
: Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(json))).ToLowerInvariant();
string signedPath = Uri.UnescapeDataString(path.Split('?')[0]);
string stringToSign = $"{method.Method.ToUpperInvariant()}:{signedPath}:{timestamp}:{nonce}:{bodyHash}";
using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(apiToken));
string signature = Convert.ToHexString(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign))).ToLowerInvariant();
request.Headers.Add("X-Timestamp", timestamp);
request.Headers.Add("X-Nonce", nonce);
request.Headers.Add("X-Signature", signature);
using var response = await _client.SendAsync(request);
string text = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode == false)
throw new HttpRequestException($"{(int)response.StatusCode}: {text}");
return string.IsNullOrEmpty(text) ? default
: JsonSerializer.Deserialize<T>(text, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
}
}
// var api = new FileFlowsApi("http://localhost:19200", "YOUR_API_TOKEN");
// var status = await api.SendAsync<JsonElement>(HttpMethod.Get, "/api/status");
Replace http://localhost:19200 with the address of your FileFlows server, and YOUR_API_TOKEN with the token from Config > API. Load the token from configuration or an environment variable rather than hard coding it.
Usage
using System.Text.Json;
var api = new FileFlowsApi("http://localhost:19200", "YOUR_API_TOKEN");
// a GET request
var status = await api.SendAsync<JsonElement>(HttpMethod.Get, "/api/status");
Console.WriteLine(status);
To send a JSON body, pass it as the body argument. The client serializes it once, and signs and sends that same JSON text.