Calling a Language Model From JavaScript

Stephen Gruppetta 23 Lessons 7h 16m Updated intermediate ai

In this course, you’ll call a language model from your own JavaScript code and treat it as what it is: an outside service that costs money on every call, can fail, and can return a reply that’s wrong. You’ll follow Leaf & Kettle Tea, a small online tea importer, and build a script that drafts replies to its customer emails.

You’ll learn how to:

  • Get an API key, set a spending limit, and load your settings from the environment
  • Send a request with fetch() and read the model’s reply, stop reason, and token usage
  • Build one small module that works with OpenAI or Anthropic
  • Keep your API key out of your code and your Git history
  • Price every call from its token usage and catch replies that were cut off
  • Retry rate limits and server errors with backoff, and stop a call that takes too long

You’ll finish with a project: a script that drafts a reply to each email in the shop’s inbox, prints the cost of the batch, and holds back any draft that was cut off, so a person can check every reply before it’s sent.

This course is for JavaScript developers who know fetch(), async and await, error handling, and Node scripts with npm, and who’ve used AI chat tools but never built on a model. You’ll need Node 26, a terminal, an editor, and a key for OpenAI or Anthropic. Following the whole course costs around ten cents, and the exercises need no key at all.

What’s Included:

  • 23 Lessons
  • Video Subtitles and Full Transcripts
  • Translated Subtitles in 8 Languages
  • 2 Downloadable Resources
  • Interactive Quiz to Check Your Progress
  • 5 Hands-On Coding Exercises
  • Q&A With JavaScript Experts: Ask a Question
  • Certificate of Completion

Downloadable Resources:

Related Learning Paths:

About Stephen Gruppetta

Stephen obtained a PhD in physics and worked as a physicist in academia for over a decade before becoming a programming educator. He's constantly looking for simple ways to explain complex things.

» More about Stephen

Each tutorial at RealJS is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

← Browse All Courses