使用API或应用程序编程接口对于创建当代软件至关重要。他们从不同的平台和服务提供申请对应用通信,数据共享以及服务访问。 API可以简化您的开发过程并节省时间,无论您是您创建移动应用程序,Web应用程序还是其他类型的软件。本文将研究您应该在2024年应了解的十个免费API,提供代码示例,以帮助您了解如何使用它们,并浏览一些用例。
通过为您的应用程序提供预制的建筑物,API简化了开发过程。为了管理付款,天气信息,用户识别等功能,您可以集成当前服务,而不是从头开始创建它们。没有提供高级服务资金的初创公司,业余爱好者和小型企业可能会从免费的API中受益。
这是您应该知道的前10个免费API:
openweather api
开放式API是用于访问实时天气数据的最受欢迎的免费API之一。它使您可以为任何城市或地区检索当前的天气,预测和历史天气数据。
导入请求 api_key =“ your_api_key” 城市=“伦敦” url = f“ http://api.openweathermap.org/data/2.5/weather? 响应= requests.get(url) weather_data = wendesp.json() 打印(f“城市:{weather_data ['name']}”) 打印(f“天气:{weather_data ['weather'] [0] ['description']}”)
当前天气数据
最多16天的天气预报
import requests api_key = "your_api_key" city = "London" url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}" response = requests.get(url) weather_data = response.json() print(f"City: {weather_data['name']}") print(f"Weather: {weather_data['weather'][0]['description']}")
github api
GitHub API是与GitHub存储库进行交互的绝妙工具。您可以自动化任务,例如管理问题,拉请请求,甚至为存储库事件设置Webhooks。
用例
GitHub API对于从事开源项目,自动化存储库管理以及将版本控制功能集成到其应用程序的开发人员至关重要。
中
免费tier提供对公共存储库的无限访问
参考:github api documentation
[2
用例
import requests api_key = "your_api_key" city = "London" url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}" response = requests.get(url) weather_data = response.json() print(f"City: {weather_data['name']}") print(f"Weather: {weather_data['weather'][0]['description']}")导入请求 api_key =“ your_api_key” url = f“ https://newsapi.org/v2/top-headlines? 响应= requests.get(url) news = wendesp.json() 对于新闻['文章']中的文章: 打印(f“ title:{atrate ['title']}”)
从数千个新闻来源
访问头条新闻
按主题,区域或出版物进行过滤新闻免费tier允许每天1000个请求
参考:newsapi documentation
用例
使用Twitter API监视趋势,获取用户推文或跟踪特定主题标签或主题的参与度。这对于社交媒体仪表板,内容营销工具和情感分析特别有用。
中获取用户推文导入斜纹 api_key =“ your_api_key” api_secret =“ your_api_secret” auth = twepy.appauthhandler(api_key,api_secret) api = twepy.api(auth) tweets = api.user_timeline(screat_name =“ elonmusk”,count = 5) 对于推文中的推文: print(f“ {tweet.user.screen_name}:{tweet.text}”)
访问公共推文和用户数据
import requests api_key = "your_api_key" url = f"https://newsapi.org/v2/top-headlines?country=us&apiKey={api_key}" response = requests.get(url) news = response.json() for article in news['articles']: print(f"Title: {article['title']}")
免费层提供对公共推文的访问
参考:Twitter API Documentation
coingecko api
Coingecko API提供加密货币市场数据,包括实时价格,交易量,市值和历史数据。它支持6000多个加密货币。
用例实时加密货币价格
支持超过6000个加密货币
免费tier提供了对各种端点的访问
参考:Coingecko API Documentation
import tweepy api_key = "your_api_key" api_secret = "your_api_secret" auth = tweepy.AppAuthHandler(api_key, api_secret) api = tweepy.API(auth) tweets = api.user_timeline(screen_name="elonmusk", count=5) for tweet in tweets: print(f"{tweet.user.screen_name}: {tweet.text}")openai api
OpenAI API提供了对GPT-4等功能强大的AI模型的访问权限,允许开发人员构建生成文本,回答问题甚至创建对话代理的应用程序。
用例
导入OpenAi OpenAI.API_KEY =“ your_api_key” 提示=“解释在Web开发中使用API的好处。” 响应= openai.completion.create( 引擎=“ text-davinci-003”, 提示=提示, max_tokens = 100 ) 打印(响应。选择[0] .Text.Strip())
基于AI的文本生成和处理
参考:OpenAI API Documentation
Firebase API是一个综合平台,用于构建和运行Web和移动应用程序,提供实时数据库,身份验证,托管和云功能。
用例
import requests url = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,ethereum&vs_currencies=usd" response = requests.get(url) data = response.json() print(f"Bitcoin: ${data['bitcoin']['usd']}") print(f"Ethereum: ${data['ethereum']['usd']}")
const firebase = require('firebase/app'); 需要('Firebase/Database'); const firebaseconfig = { Apikey:“ your_api_key”, authdomain:“ your_project.firebaseapp.com”, 数据库:“ https://your_project.firebaseio.com”, }; firebase.initializeapp(firbaseconfig); const db = firebase.database(); db.ref('users/')。设置({ 用户名:“ John Doe”, 电子邮件:“ [email protected]” });
实时数据库
身份验证服务
免费tier为小规模应用程序提供基本功能
参考:firebase api document
NASA API提供了大量空间数据的访问,包括图像,视频以及有关行星,星星和其他天体对象的信息。
用例NASA API非常适合可视化或使用空间数据的教育应用程序,以空格为主题的网站以及应用程序。
中获取NASA日期的图像
导入请求
api_key =“ your_api_key”
url = f“ https://api.nasa.gov/planetary/apod?api_key = {papi_key}”
响应= requests.get(url)
data = reverse.json()
打印(f“ title:{data ['title']}”)
打印(f“ url:{data ['url']}”)
import requests api_key = "your_api_key" city = "London" url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}" response = requests.get(url) weather_data = response.json() print(f"City: {weather_data['name']}") print(f"Weather: {weather_data['weather'][0]['description']}")
免费访问公共数据集
的免费层参考:NASA API Documentation
Jikan API
Jikan API是免费的API
用例详细详细的动漫和漫画信息
支持通过流行,受欢迎程度和播放状态进行过滤
免费层提供对所有公共端点的无限访问
参考:Jikan API Documentation
import tweepy api_key = "your_api_key" api_secret = "your_api_secret" auth = tweepy.AppAuthHandler(api_key, api_secret) api = tweepy.API(auth) tweets = api.user_timeline(screen_name="elonmusk", count=5) for tweet in tweets: print(f"{tweet.user.screen_name}: {tweet.text}")
猫事实API是一种有趣而古怪的API,提供了有关猫的随机事实。这是一个轻松的API,但对于想要为用户提供有趣而有趣的内容的应用程序和网站来说,这是一个很好的补充。
用例此API非常适合娱乐应用程序,有趣的小部件,甚至是您的用户每日有趣的事实。
随机猫事实
免费tier提供无限访问
API是强大的工具,可以显着增强应用程序的功能,而无需您从头开始构建所有内容。本文中涵盖的10个免费API可以帮助您添加诸如天气更新,加密货币数据,社交媒体集成,甚至是AI-Driven的文本生成的功能。
这些API不仅提供免费的层次,还为各个级别的开发人员提供了强大的文档和易于使用的接口。无论您是构建简单的应用程序还是复杂的平台,这些API都可以帮助您节省时间,并专注于为用户构建独特功能。
集成这些API只是编写几行代码的问题,如示例所示。现在,您知道要探索哪个API,开始与它们进行实验,以了解如何将您的开发过程提升到一个新的水平!
免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。
Copyright© 2022 湘ICP备2022001581号-3