Skip to content

Getting Started: .NET (C#)

Use .NET 6+ with HttpClient to call the BetterPrompt API.

csharp
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        var apiKey = Environment.GetEnvironmentVariable("BETTERPROMPT_API_KEY") ?? "YOUR_API_KEY";
        var promptVersionId = Environment.GetEnvironmentVariable("BP_PROMPT_VERSION_ID") ?? "YOUR_PROMPT_VERSION_ID";

        var json = $"{{\"promptVersionId\":\"{promptVersionId}\",\"inputs\":{{\"textInputs\":{{\"yourField\":\"value\"}}}}}}";

        using var client = new HttpClient();
        using var request = new HttpRequestMessage(HttpMethod.Post, "https://api.betterprompt.me/v1/runs");
        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
        request.Content = new StringContent(json, Encoding.UTF8, "application/json");

        var response = await client.SendAsync(request);
        Console.WriteLine(await response.Content.ReadAsStringAsync());
    }
}