12 个免费且有趣的 API,你必须在你的业余项目中使用
厌倦了千篇一律的项目?想寻找创意和灵感的火花吗?✨
API(应用程序编程接口)就像神奇的大门,打开了浩瀚的数据和功能世界。最棒的是?它们很多都是免费的,而且使用起来非常有趣!
在这篇博客中,我将向你介绍 13 个很棒的 API,它们能为你的业余项目增添新的维度。准备好探索、实验,创造一些真正独一无二的东西吧!
1. Cat API
获取随机的猫图像、事实或特定品种,为您的项目增添一丝猫科动物的乐趣。
“与猫一起度过的时光永远不会被浪费。”——西格蒙德·弗洛伊德
示例代码(JavaScript)
fetch('https://api.thecatapi.com/v1/images/search')
.then(response => response.json())
.then(data => {
const imageUrl = data[0].url;
console.log(`Here's a random cat image: ${imageUrl}`);
});
2. JokeAPI
使用 JokeAPI 为您的项目注入幽默元素。检索各种主题的笑话,或为您的用户打造个性化的笑话体验。
“笑声让人瞬间放松。”——米尔顿·伯利
示例代码(JavaScript)
fetch('https://v2.jokeapi.dev/joke/Any')
.then(response => response.json())
.then(data => {
console.log(`Joke: ${data.joke}`);
});
3.Pexels API
通过 Pexels API 访问海量高质量免版税图片库。非常适合需要惊艳视觉效果的项目。
“一张图片胜过千言万语。”——未知
示例代码(JavaScript)
const apiKey = 'your_api_key_here';
const url = `https://api.pexels.com/v1/curated?per_page=1`;
fetch(url, {
headers: {
Authorization: apiKey,
},
})
.then(response => response.json())
.then(data => {
const imageUrl = data.photos[0].src.original;
console.log(`Here's a curated image: ${imageUrl}`);
});
4.通知单API
使用 Advice Slip API 获取随机建议。这是一个趣味十足的附加功能,让用户在娱乐的同时还能获取信息。
“预测未来的最好方法就是创造未来。”——彼得·德鲁克
示例代码(JavaScript)
fetch('https://api.adviceslip.com/advice')
.then(response => response.json())
.then(data => {
const advice = data.slip.advice;
console.log(`Advice: ${advice}`);
});
5. Giphy API
使用 Giphy API 为您的项目增添动感元素。获取热门 GIF 或搜索特定 GIF,提升用户体验。
“人生苦短,不如做成动图。”——未知
示例代码(JavaScript)
const apiKey = 'your_api_key_here';
const searchTerm = 'excited';
fetch(`https://api.giphy.com/v1/gifs/random?api_key=${apiKey}&tag=${searchTerm}`)
.then(response => response.json())
.then(data => {
const gifUrl = data.data.images.original.url;
console.log(`Excitement GIF: ${gifUrl}`);
});
6.趣味翻译 API
使用趣味翻译 API,将文本转换为各种趣味翻译。用有趣的语言转换为您的内容增添趣味。
“语言是文化的路线图。”——丽塔·梅·布朗
示例代码(JavaScript)
const apiKey = 'your_api_key_here';
const textToTranslate = 'Hello, World!';
fetch(`https://api.funtranslations.com/translate/yoda.json?text=${textToTranslate}`, {
headers: {
'X-Funtranslations-Api-Secret': apiKey,
},
})
.then(response => response.json())
.then(data => {
const translatedText = data.contents.translated;
console.log(`Yoda Translation: ${translatedText}`);
});
使用开放琐事数据库 API,向用户挑战琐事。您可以获取随机问题,也可以自定义查询,打造个性化的琐事体验。
“知识就是力量。”——弗朗西斯·培根
示例代码(JavaScript)
fetch('https://opentdb.com/api.php?amount=1&type=multiple')
.then(response => response.json())
.then(data => {
const question = data.results[0].question;
console.log(`Trivia Question: ${question}`);
});
8. Jikan API
对于动漫爱好者来说,Jikan API 提供了丰富的动漫和漫画数据。您可以检索有关剧集、角色等的详细信息。
“动漫不是一种类型,而是一种艺术形式。”——未知
示例代码(JavaScript)
const animeTitle = 'One Piece';
fetch(`https://api.jikan.moe/v3/search/anime?q=${animeTitle}&page=1`)
.then(response => response.json())
.
then(data => {
const animeDetails = data.results[0];
console.log(`Anime Title: ${animeDetails.title}`);
});
使用 Cocktail DB API 将调酒技术融入你的业余项目。获取丰富的鸡尾酒配方、配料等信息,激发完美饮品的灵感。
“生活是美好的。”——未知
示例代码(JavaScript)
fetch('https://www.thecocktaildb.com/api/json/v1/1/random.php')
.then(response => response.json())
.then(data => {
const cocktailName = data.drinks[0].strDrink;
console.log(`Random Cocktail: ${cocktailName}`);
});
10.老爸笑话 API
使用“老爸笑话 API”为您的项目注入一些老爸幽默元素。检索各种老爸笑话,让您的用户乐在其中。
“没有笑声的一天是浪费的一天。”——查理·卓别林
示例代码(JavaScript)
fetch('https://icanhazdadjoke.com/', {
headers: {
Accept: 'application/json',
},
})
.then(response => response.json())
.then(data => {
const dadJoke = data.joke;
console.log(`Dad Joke: ${dadJoke}`);
});
11.SpaceX API
使用 SpaceX API 探索太空奇观。访问 SpaceX(太空探索领域的重要参与者)提供的发射、火箭、太空舱等数据。
“地球是人类的摇篮,但人类不能永远停留在摇篮里。”——康斯坦丁·齐奥尔科夫斯基
示例代码(JavaScript)
fetch('https://api.spacexdata.com/v4/launches/next')
.then(response => response.json())
.then(data => {
const missionName = data.name;
console.log(`Next SpaceX Launch: ${missionName}`);
});
12. Recipe Puppy API
使用 Recipe Puppy API,为您的业余项目带来烹饪乐趣。访问海量食谱和烹饪灵感。
“烹饪是可见的爱。”——未知
示例代码(JavaScript)
const ingredient = 'chicken';
fetch(`http://www.recipepuppy.com/api/?i=${ingredient}&p=1`)
.then(response => response.json())
.then(data => {
const recipeTitle = data.results[0].title;
console.log(`Chicken Recipe: ${recipeTitle}`);
});
敬请期待本博客下一篇,了解更多精彩 API!我们将深入探讨如何根据姓名预测年龄、根据文本生成艺术作品等等。准备好利用 API 的强大功能解锁无限可能吧!
让我们听听你的意见!你之前用过这些 API 吗?你有什么好玩的项目想法吗?请在下方评论区分享你的想法!
文章来源:https://dev.to/mukeshkuiry/12-free-and-fun-api-for-your-next-project-5eem