一、人工智能与大模型API
AI API是当下最热门的接口类别,许多厂商都提供了免费额度。
1. OpenAI API
2. Google Gemini API
3. Hugging Face Inference API
python
# Hugging Face 免费API调用示例
import requests
API_URL = "https://api-inference.huggingface.co/models/sentence-transformers/all-MiniLM-L6-v2"
headers = {"Authorization": "Bearer hf_your_token_here"}
def get_embedding(text):
response = requests.post(API_URL, headers=headers, json={"inputs": text})
return response.json()
embedding = get_embedding("Hello, world!")
print(f"向量维度: {len(embedding)}")4. Cohere API
5. Replicate API
二、天气与环境API
6. OpenWeatherMap
7. WeatherAPI.com
8. Open-Meteo
python
# Open-Meteo 免费天气查询(无需API Key)
import requests
def get_weather(lat, lon):
url = f"https://api.open-meteo.com/v1/forecast"
params = {
"latitude": lat,
"longitude": lon,
"current": "temperature_2m,wind_speed_10m",
"timezone": "auto"
}
resp = requests.get(url, params=params)
return resp.json()
# 查询北京天气
weather = get_weather(39.9, 116.4)
print(f"当前温度: {weather['current']['temperature_2m']}°C")9. AirVisual (IQAir)
三、金融与加密货币API
10. Alpha Vantage
11. CoinGecko API
12. Exchange Rate API
13. Frankfurter API
14. Nasdaq Data Link
四、地图与地理位置API
15. Nominatim (OpenStreetMap)
16. Open Route Service
17. IPAPI
18. REST Countries
javascript
// REST Countries API 查询国家信息
async function getCountryInfo(name) {
const resp = await fetch(`https://restcountries.com/v3.1/name/${name}`);
const data = await resp.json();
const country = data[0];
console.log(`国家: ${country.name.common}`);
console.log(`首都: ${country.capital?.[0] || '无'}`);
console.log(`人口: ${country.population.toLocaleString()}`);
console.log(`货币: ${Object.keys(country.currencies).join(', ')}`);
}
getCountryInfo("china");五、图像与多媒体处理API
19. Unsplash API
20. Pexels API
21. TMDB API
22. Spotify Web API
23. Giphy API
六、数据与知识API
24. Wikipedia API
25. NASA API
26. NewsAPI
27. The Guardian API
28. Datamuse API
29. NumbersAPI
python
# NumbersAPI 获取数字趣味事实
import requests
def get_number_fact(number):
url = f"http://numbersapi.com/{number}"
resp = requests.get(url)
return resp.text
print(get_number_fact(42))
# 输出示例: 42 is the number of little squares that make up ...七、开发工具与基础设施API
30. GitHub API
31. GitLab API
32. Discogs API
33. QR Server API
html
<!-- QR Server API 直接生成二维码 -->
<img src="https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=https://example.com"
alt="QR Code" />34. Ipify API
35. SMTP2GO / Resend
36. Twilio Verify
八、实用工具API
37. Hacker News API
38. Reddit API
39. Stack Exchange API
40. jokes.one API
41. Advice Slip API
42. Bored API
python
# Bored API 获取随机活动建议
import requests
def get_random_activity():
resp = requests.get("https://www.boredapi.com/api/activity")
data = resp.json()
return f"建议活动: {data['activity']}(类型: {data['type']})"
print(get_random_activity())九、翻译与文本处理API
43. MyMemory Translation API
44. LibreTranslate
45. LangDetect API
十、其他实用API
46. Open Library API
47. PokeAPI
48. JSONPlaceholder
49.ReqRes
50. Carbon Interface API
使用免费API的最佳实践
1. 做好缓存
免费额度有限,务必做好本地缓存,避免重复请求相同数据。
python
import functools
import time
@functools.lru_cache(maxsize=128)
def cached_api_call(endpoint, ttl_hash=None):
"""带TTL缓存的API调用"""
# ttl_hash用于控制缓存过期
resp = requests.get(endpoint)
return resp.json()
def get_ttl_hash(seconds=300):
"""生成基于时间的hash,用于缓存过期控制"""
return int(time.time() / seconds)2. 实现优雅降级
当API不可用或额度耗尽时,要有备选方案。
3. 监控配额使用
大多数API都会在响应头中返回剩余配额信息,建议记录并告警。
4. 遵守使用条款
仔细阅读每个API的使用条款,特别是商用限制和署名要求。
总结
以上50个免费API覆盖了开发中绝大多数常见需求。建议你根据自己的项目方向,挑选3-5个深度使用,而不是泛泛了解。API的选择标准应该是:文档清晰、社区活跃、免费额度足够、有升级付费选项以备业务增长。
善用这些免费资源,你可以在几乎零成本的情况下构建出令人惊艳的产品。祝你在2026年的开发之旅中事半功倍!
💬 评论区 (0)
暂无评论,快来抢沙发吧!