Use REST API endpoint over SDK for OpenAI
Dom Sipowicz

Dom Sipowicz @sip

About: AI + ▲Vercel + Next.js + WebPerf

Location:
London, UK
Joined:
Oct 18, 2022

Use REST API endpoint over SDK for OpenAI

Publish Date: Apr 18 '23
4 1

It is common for most projects to use only one method from the OpenAI JavaScript SDK’s implementation, so why not use native fetch for the this one REST endpoint instead?

Question?

In AI web projects or any web AI template, if you want to access OpenAI API, why are you using openai npm module rather than the REST API endpoints directly?

I personally think using REST endpoints is the best possible approach in terms of runtimes compatibility.

Using direct REST API endpoint instead of OpenAI SDK

For JavaScript AI projects the default recommendation should be to use native fetch and move to SDK later if necessary.

Performance benefits are clear:

  • Reduced bundle size
  • Using native fetch for compatibility with any runtime (edge, workers, deno, browser)
  • streaming
  const res = await fetch('https://api.openai.com/v1/chat/completions', {
    headers: requestHeaders,
    method: 'POST',
    body: JSON.stringify(payload),
  })
Enter fullscreen mode Exit fullscreen mode

If you are running long-running AI calls, remember to use Edge Functions instead of Serverless Functions:

References:


Godspeed

PS. Follow me on Twitter or LinkedIn
https://twitter.com/dom_sipowicz
https://www.linkedin.com/in/dominiksipowicz/

Comments 1 total

Add comment