r/learnjavascript • u/SergioCoitino • 29d ago
I built a free image API for SWAPI (The Star Wars API) to use in JS/React learning projects
SWAPI (swapi.dev) is one of the most popular APIs when learning how to fetch() data in JavaScript or React, but it only returns JSON text without any pictures.
Also, the old visual guide many tutorials relied on currently fails with SSL certificate errors.
To help anyone practicing API integration, I built a free static image CDN where every image matches the exact ID of the SWAPI resource:
const getSwapiImage = (category, swapiUrl) => { const id = swapiUrl.split('/').filter(Boolean).pop(); return https://starwarsgallery.netlify.app/assets/${category}/${id}.jpg; };
// Example usage: const res = await fetch('https://swapi.dev/api/people/1/'); const luke = await res.json(); const imageUrl = getSwapiImage('people', luke.url); console.log(imageUrl); // Output: "https://starwarsgallery.netlify.app/assets/people/1.jpg"
Key features:
- Same IDs as SWAPI: works across people, planets, species, starships, and vehicles (238 images).
- CORS enabled: Access-Control-Allow-Origin: * works in fetch(), img tags, canvas, etc.
- Real 404s: missing IDs return an HTTP 404 so your img onError logic works correctly.
- Index JSON: available at https://starwarsgallery.netlify.app/api/images.json
Full documentation & examples: https://starwarsgallery.netlify.app/swapi-image-api/
Live visual gallery: https://starwarsgallery.netlify.app/
Hope this helps anyone building Star Wars practice projects! Feedback and suggestions are very welcome.