Announcing OpenAI on JSR
JSR is a modern open source JavaScript registry that simplifies publishing and importing JavaScript and TypeScript modules. JSR supports publishing TypeScript source code, auto-generating documentation and type definition files, provenance attestation for more security, and can be used with npm-like package managers. You can use JSR modules not just with Deno, but in other runtimes like Node and environments like the browser and the edge. JSR has seen continued growth since launch, with over 400 new packages published weekly.
We’re thrilled to announce that the OpenAI JavaScript SDK is now available on JSR.
OpenAI is a leading development platform for building AI products and experiences using a diverse set of models including GPT, DALL-E, and more. It can also be used across any JavaScript environment, such as browsers and various runtimes.
Using OpenAI’s SDK via JSR offers an unbeatable developer experience, with first class TypeScript support, auto-generated documentation right in your code editor, and more.
🚨️ Looking to publish your own libraries on JSR? 🚨️
Try Stainless - the comprehensive API platform that ingests OpenAPI specs to autogenerate SDKs including JSR-ready TypeScript modules. In fact, this OpenAI SDK was built using Stainless.
Installing OpenAI
You can get started with OpenAI using the deno add
command:
deno add jsr:@openai/openai
Or using npm:
npx jsr add @openai/openai
The first command will generate and add @openai
to a deno.json
file, while
the second will generate and add @openai
to a package.json
file. Looking at
our deno.json
:
// deno.json
{
"imports": {
"@openai/openai": "jsr:@openai/openai@4.80.0"
}
}
Let’s use the OpenAI SDK to create a human-like response based on a text prompt.
In our main.ts
file, we can write:
import OpenAI from "@openai/openai";
const openai = new OpenAI();
const completion = await openai.chat.completions.create({
model: "gpt-4o-mini",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{
role: "user",
content: "Write a haiku about recursion in programming.",
},
],
});
console.log(completion.choices[0].message);
Next, let’s create an OpenAI key here and set it as an environment variable. In Mac/Linux, that looks like this:
export OPENAI_API_KEY="your_api_key_here"
Finally, let’s run it:
$ deno -A main.ts
{
role: "assistant",
content: "Functions call themselves, \n" +
"Layers deep in logic's dance, \n" +
"Infinite embrace. ",
refusal: null
}
It works! (And if you’re hitting the 429 error code, you may need to upgrade.)
Check out OpenAI’s documentation to learn more about working with their various models and creating AI driven experiences.
What’s next?
With OpenAI’s JavaScript SDK now on JSR, it’s even easier to add AI experiences to your products or build AI products.
🚨️ Read more about JSR 🚨️