10 must use APIs for Your Next Python Project with code example for each
--
Here are 10 killer APIs along with code examples for each:
- OpenWeatherMap API:
Code Example:
import requests
api_key = 'YOUR_API_KEY'
city = 'London'
response = requests.get(f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}')
data = response.json()
temperature = data['main']['temp']
print(f"The temperature in {city} is {temperature} Kelvin.")
2. Twilio API:
Code Example:
from twilio.rest import Client
account_sid = 'YOUR_ACCOUNT_SID'
auth_token = 'YOUR_AUTH_TOKEN'
client = Client(account_sid, auth_token)
message = client.messages.create(
body='Hello, from Twilio!',
from_='+1234567890',
to='+0987654321'
)
print(f"Message sent with SID: {message.sid}")
3. Spotify API:
Code Example:
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
client_id = 'YOUR_CLIENT_ID'
client_secret = 'YOUR_CLIENT_SECRET'
sp = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials(client_id, client_secret))
results = sp.search(q='artist:Ed Sheeran', type='artist')
artist = results['artists']['items'][0]
print(f"Name: {artist['name']}, Popularity: {artist['popularity']}")
4. Google Maps API:
Code Example:
import googlemaps
api_key = 'YOUR_API_KEY'
gmaps = googlemaps.Client(api_key)
geocode_result = gmaps.geocode('1600 Amphitheatre Parkway, Mountain View, CA')
print(geocode_result[0]['formatted_address'])
5. GitHub API:
Code Example:
import requests
username = 'YOUR_USERNAME'
response = requests.get(f'https://api.github.com/users/{username}')
data = response.json()
print(f"Username: {data['login']}, Public Repositories: {data['public_repos']}")
6. TMDb API:
Code Example:
import requests
api_key = 'YOUR_API_KEY'
response = requests.get(f'https://api.themoviedb.org/3/movie/popular?api_key={api_key}')
data = response.json()
for movie in data['results']:
print(f"Title: {movie['title']}, Popularity: {movie['popularity']}")
7. NASA API:
Code Example:
import requests
api_key = 'YOUR_API_KEY'
response = requests.get(f'https://api.nasa.gov/planetary/apod?api_key={api_key}')
data = response.json()
print(f"Title: {data['title']}")
print(f"Explanation: {data['explanation']}")
8. NewsAPI:
Code Example:
import requests
api_key = 'YOUR_API_KEY'
response = requests.get(f'https://newsapi.org/v2/top-headlines?country=us&apiKey={api_key}')
data = response.json()
for article in data['articles']:
print(f"Title: {article['title']}, Source: {article['source']['name']}")
9. CoinGecko API:
Code Example:
import requests
OpenAI GPT-3 API:
Code Example:
import openai
api_key = 'YOUR_API_KEY'
prompt = 'Once upon a time'
openai.api_key = api_key
response = openai.Completion.create(
engine='text-davinci-003',
prompt=prompt,
max_tokens=50
)
generated_text = response.choices[0].text.strip()
print(f"Generated Text: {generated_text}")
Please note that some code examples are simplified for brevity, and you may need to explore the API documentation for each service to discover more functionalities and parameters that suit your project’s requirements.
Hope you will love this!