using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace OpenAiTranscriptionExample
public static void Main(string[] args)
string apiKey = "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";
string audioFilePath = "https://inhouse.briskstaruat.com/nikita/audio14.mp3";
var transcription = TranscribeAudioAsync(apiKey, audioFilePath).GetAwaiter().GetResult();
Console.WriteLine("Transcription: " + transcription);
Console.WriteLine("An error occurred: " + ex.Message);
private static async Task<string> TranscribeAudioAsync(string apiKey, string audioFilePath)
var client = new HttpClient();
client.DefaultRequestHeaders.UserAgent.ParseAdd("MyDotNetFiddleApp/1.0 (nikitak.briskstar@gmail.com)");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
byte[] fileContent = await client.GetByteArrayAsync(audioFileUrl);
var content = new MultipartFormDataContent();
var fileByteArrayContent = new ByteArrayContent(fileContent);
fileByteArrayContent.Headers.ContentType = new MediaTypeHeaderValue("audio/mp3");
content.Add(fileByteArrayContent, "file", audioFilePath);
content.Add(new StringContent("whisper-1"), "model");
HttpResponseMessage response = await client.PostAsync("https://api.openai.com/v1/audio/transcriptions", content);
if (response.IsSuccessStatusCode)
string jsonResponse = await response.Content.ReadAsStringAsync();
var transcriptionResponse = JsonConvert.DeserializeObject<dynamic>(jsonResponse);
return transcriptionResponse.text;
throw new Exception(String.Format("API request failed with status code {0}: {1}", response.StatusCode, await response.Content.ReadAsStringAsync()));