I'm trying to set up a Semantic Kernel project and I would like to connect it to the OpenRouter api. To the best of my knowledge, their API should be compatible with the OpenAI API, therefore a connection should work. I used
var httpClient = new HttpClient
{
Timeout = TimeSpan.FromMinutes(10)
};
builder.AddOpenAIChatCompletion(
modelId:"deepseek/deepseek-r1",
apiKey: @"my Open Router Api Key",
endpoint: new Uri("https://openrouter.ai/api/v1"),
httpClient: httpClient);
I get a 404 error when I try this and I don't know why. When I use the OpenAI API everything works.
I also tried using this HttpClientHandler so that the requests go out to OpenRouter but it didn't work either and I get a 405 error:
public class MyHttpMessageHandler : HttpClientHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request.RequestUri != null && request.RequestUri.Host.Equals("api.openai.com", StringComparison.OrdinalIgnoreCase))
{
request.RequestUri = new Uri($"https://openrouter.ai/api/v1");
}
return base.SendAsync(request, cancellationToken);
}
}