using System.Net.Http.Headers; using System.Text.Json; var baseUrl = (Environment.GetEnvironmentVariable("PUBLIC_API_BASE_URL") ?? "https://api.compudime.com").TrimEnd('/'); var apiKey = Environment.GetEnvironmentVariable("PUBLIC_API_KEY"); if (string.IsNullOrWhiteSpace(apiKey)) { throw new InvalidOperationException("Set PUBLIC_API_KEY to before running this example."); } using var http = new HttpClient(); http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey); http.DefaultRequestHeaders.Add("X-Request-Id", "csharp-products-001"); var fields = Uri.EscapeDataString("productCode,description,price"); using var response = await http.GetAsync($"{baseUrl}/v1/products?fields={fields}&limit=50"); var body = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { using var problemJson = JsonDocument.Parse(body); var root = problemJson.RootElement; throw new InvalidOperationException($"Public API problem JSON {root.GetProperty("code").GetString()} ({root.GetProperty("requestId").GetString()}): {root.GetProperty("detail").GetString()}"); } using var productsJson = JsonDocument.Parse(body); foreach (var product in productsJson.RootElement.GetProperty("products").EnumerateArray()) { Console.WriteLine($"{product.GetProperty("productCode").GetString()} {product.GetProperty("description").GetString()} {product.GetProperty("price").GetString()}"); } if (productsJson.RootElement.TryGetProperty("nextCursor", out var nextCursor)) { Console.WriteLine($"Use nextCursor to request the next page: {nextCursor.GetString()}"); }