> Canonical URL: https://codingcat.dev/course/svelte/lesson/how-to-call-an-api

# Learn Svelte: The Ultimate Guide - How to call an API | Learn Svelte: The Ultimate Guide | CodingCat.dev Course

[🧶 Learn Svelte: The Ultimate Guide](/course/svelte)Section 1.10 · Lesson 10 of 14[![](/logos/tags/svelte-symbol-light.svg)![](/logos/tags/svelte-symbol-dark.svg)Svelte](/topic/svelte)[![](/logos/tags/javascript-symbol-light.svg)![](/logos/tags/javascript-symbol-dark.svg)JavaScript](/topic/javascript)[![](/logos/tags/css-symbol-light.svg)![](/logos/tags/css-symbol-dark.svg)CSS](/topic/css)[← Prev Lesson](/course/svelte/lesson/css-basics)[Next Lesson →](/course/svelte/lesson/await-block)

# Learn Svelte: The Ultimate Guide - How to call an API

![](https://cdn.sanity.io/media-libraries/mlttp0BZbCpV/images/d50b57c3597cbcaecc285e7be769af410ff94856-1920x1080.png?w=1920&h=1080&q=82&fit=max&auto=format)[@CodingCatDev62.5K / 100K Subscribers Goal](https://www.youtube.com/@codingcatdev?sub_confirmation=1)[Subscribe on YouTube→](https://www.youtube.com/@codingcatdev?sub_confirmation=1)[Watch on YouTube App ↗](https://www.youtube.com/watch?v=O-72Vwpe5Ag&list=UUnKZ8gEb78zXKMi1ns-IQ2g)Lesson 10 / 14Course Lessons Outline▾1Svelte
- [1.1Learn Svelte: The Ultimate Guide - What is Svelte?](/course/svelte/lesson/what-is-svelte)
- [1.2Learn Svelte: The Ultimate Guide - Getting Started](/course/svelte/lesson/getting-started)
- [1.3Learn Svelte: The Ultimate Guide - Starting to Code](/course/svelte/lesson/starting-to-code)
- [1.4Learn Svelte: The Ultimate Guide - Components and Props](/course/svelte/lesson/components-and-props)
- [1.5Learn Svelte: The Ultimate Guide - Inputs Bindings and Reactivity](/course/svelte/lesson/inputs-bindings-and-reactivity)
- [1.6Learn Svelte: The Ultimate Guide - Basic Events](/course/svelte/lesson/basic-events)
- [1.7Learn Svelte: The Ultimate Guide - Component Events and Forwarding](/course/svelte/lesson/component-events-forwarding)
- [1.8Learn Svelte: The Ultimate Guide - Conditionals and Loops](/course/svelte/lesson/conditionals-and-loops)
- [1.9Learn Svelte: The Ultimate Guide - CSS Basics](/course/svelte/lesson/css-basics)
- [1.10Learn Svelte: The Ultimate Guide - How to call an API](/course/svelte/lesson/how-to-call-an-api)
- [1.11Learn Svelte: The Ultimate Guide - Await Block](/course/svelte/lesson/await-block)
- [1.12Learn Svelte: The Ultimate Guide - Transitions and Animations](/course/svelte/lesson/transitions-and-animations)
- [1.13Learn Svelte: The Ultimate Guide - Component Lifecycle](/course/svelte/lesson/component-lifecycle)
- [1.14Learn Svelte: The Ultimate Guide - Data Stores](/course/svelte/lesson/data-stores)

## Fetching Data from a Web API

To fetch data from a web API, we can use the fetch API. The fetch API is a modern JavaScript API that makes it easy to make HTTP requests.

To use the fetch API, we first need to create a request object. The request object specifies the URL of the API and the HTTP method that we want to use.

Once we have created a request object, we can use the fetch() method to make the request. The fetch() method returns a promise that resolves to the response object.

The response object contains the status code, headers, and body of the response. We can use the status code to determine whether the request was successful or not.

If the request was successful, we can use the headers and body of the response to get the data that we need.

Below is an example of us getting using the Fetch API to get kitten data.

Copied! 🐾

```
let kittens: { src: string; name: string }[] = [];

const getKittens = async () => {
    const res = await fetch('https://api.thecatapi.com/v1/images/search?limit=10');
    const kittenJson = await res.json();
    kittens = kittenJson.map((k: any) => {
        return { src: k.url, name: k.id };
    });
};
```

## Handling Errors

When we make a request to a web API, there is always the possibility that the request will fail. For example, the API may be down or the request may be invalid.

To handle errors, we can use the catch() method on the promise returned by the fetch() method. The catch() method takes a function as an argument. The function will be called if the request fails.

The function passed to the catch() method will receive the error object as an argument. The error object contains information about the error, such as the status code and the message.

## Caching Data

To improve the performance of our application, we can cache the data that we fetch from the web API. Caching the data means that we store the data in memory so that we don't have to fetch it from the API every time.

To cache data, we can use the cache() method on the response object. The cache() method takes a boolean value as an argument. The boolean value specifies whether we want to cache the data in memory.

If we set the boolean value to true, the response object will be cached in memory. The next time we make a request to the same API, the response object will be returned from the cache instead of being fetched from the API.

## Conclusion

In this blog post, we showed you how to use the fetch API to fetch data from a web API in Svelte. We also showed you how to handle errors and cache the data.

✏️ Visual Architecture Diagram

## how this course connects together.

Follow AJ from first principles to production-ready architecture.

Let’s build it!✏️ Visual Diagram: Learn Svelte: The Ultimate Guide: Learn Svelte: …✓ Good PatternVISUAL DIAGRAM[Diagram Walkthrough]Step-by-step diagram breakdown for Learn Svelte: The Ultimate Guide: Learn Svelte: The Ultimate Guide - How to call an API: start with Svelte, implement Learn Svelte: The Ultimate Guide, and ship to production.AAA Contrast Verified

## related deep dives.

[View All&rarr;](/blog)[![The Odin Project 1: Web Development Foundations (HTML, CSS, Flexbox & JS Basics)](https://cdn.sanity.io/images/hfh83o0w/production/fce6fcae58667ff3143db133eb6b57fbd9ec471f-1376x768.png?w=1376&q=82&fit=max&auto=format)](/course/odin-web-development-foundations)November 4, 2026[![](/logos/tags/html-symbol-light.svg)![](/logos/tags/html-symbol-dark.svg)HTML](/topic/html)[![](/logos/tags/css-symbol-light.svg)![](/logos/tags/css-symbol-dark.svg)CSS](/topic/css)[![](/logos/tags/javascript-symbol-light.svg)![](/logos/tags/javascript-symbol-dark.svg)JavaScript](/topic/javascript)[+1](/course/odin-web-development-foundations)

### [The Odin Project 1: Web Development Foundations (HTML, CSS, Flexbox & JS Basics)](/course/odin-web-development-foundations)

Master the complete Odin Project Foundations path modernized for 2026: how the web works, semantic HTML5, CSS Box Model & Flexbox, and JavaScript DOM fundamentals.

[![Alex Patterson](https://cdn.sanity.io/media-libraries/mlttp0BZbCpV/images/a606dfaa381aab08ddbe16a33fe1d3e1fb37c927-400x400.jpg?w=96&h=96&q=82&fit=max&auto=format)Alex Patterson](/author/alex-patterson)[![The Odin Project 5: Advanced HTML, CSS View Transitions & WCAG Accessibility](https://cdn.sanity.io/images/hfh83o0w/production/93736ccee5b8c088a202f319ce1223899a41f6bf-1376x768.png?w=1376&q=82&fit=max&auto=format)](/course/odin-advanced-html-css-and-a11y)November 4, 2026[![](/logos/tags/css-symbol-light.svg)![](/logos/tags/css-symbol-dark.svg)CSS](/topic/css)[#Accessibility](/topic/a11y)[![](/logos/tags/html-symbol-light.svg)![](/logos/tags/html-symbol-dark.svg)HTML](/topic/html)[+1](/course/odin-advanced-html-css-and-a11y)

### [The Odin Project 5: Advanced HTML, CSS View Transitions & WCAG Accessibility](/course/odin-advanced-html-css-and-a11y)

Build 60fps GPU-accelerated animations, multi-page Astro View Transitions, WCAG AAA focus rings, and accessible native <dialog> and Popover components.

[![Alex Patterson](https://cdn.sanity.io/media-libraries/mlttp0BZbCpV/images/a606dfaa381aab08ddbe16a33fe1d3e1fb37c927-400x400.jpg?w=96&h=96&q=82&fit=max&auto=format)Alex Patterson](/author/alex-patterson)[Course Outline](/course/svelte)

## Learn Svelte: The Ultimate Guide

10/141

### Svelte

- [1.1Learn Svelte: The Ultimate Guide - What is Svelte?](/course/svelte/lesson/what-is-svelte)
- [1.2Learn Svelte: The Ultimate Guide - Getting Started](/course/svelte/lesson/getting-started)
- [1.3Learn Svelte: The Ultimate Guide - Starting to Code](/course/svelte/lesson/starting-to-code)
- [1.4Learn Svelte: The Ultimate Guide - Components and Props](/course/svelte/lesson/components-and-props)
- [1.5Learn Svelte: The Ultimate Guide - Inputs Bindings and Reactivity](/course/svelte/lesson/inputs-bindings-and-reactivity)
- [1.6Learn Svelte: The Ultimate Guide - Basic Events](/course/svelte/lesson/basic-events)
- [1.7Learn Svelte: The Ultimate Guide - Component Events and Forwarding](/course/svelte/lesson/component-events-forwarding)
- [1.8Learn Svelte: The Ultimate Guide - Conditionals and Loops](/course/svelte/lesson/conditionals-and-loops)
- [1.9Learn Svelte: The Ultimate Guide - CSS Basics](/course/svelte/lesson/css-basics)
- [1.10Learn Svelte: The Ultimate Guide - How to call an API▶](/course/svelte/lesson/how-to-call-an-api)
- [1.11Learn Svelte: The Ultimate Guide - Await Block](/course/svelte/lesson/await-block)
- [1.12Learn Svelte: The Ultimate Guide - Transitions and Animations](/course/svelte/lesson/transitions-and-animations)
- [1.13Learn Svelte: The Ultimate Guide - Component Lifecycle](/course/svelte/lesson/component-lifecycle)
- [1.14Learn Svelte: The Ultimate Guide - Data Stores](/course/svelte/lesson/data-stores)

@CodingCatDev on YouTube62.5K / 100K

Subscribe for new course lessons and architecture deep dives every week.

[Subscribe for New Lessons →](https://www.youtube.com/@codingcatdev?sub_confirmation=1)