Skip to main content

Overview

File: src/tools/GetWeatherTool.ts The Weather Tool demonstrates external API integration using the free Open-Meteo API (no API key required).

What It Does

  • Fetches real-time weather for any city worldwide
  • Two-step process: geocoding + weather data
  • Error handling for invalid cities
  • No API key or authentication required

Complete Code

Key Concepts

1. Two-Step API Call

Why? Weather APIs need coordinates, but users provide city names.

2. URL Encoding

Always encode user input in URLs:

3. Error Handling

Check if city exists before proceeding:

4. Structured Return

Return organized data, not raw API response:

Testing

Try different cities:
  • London - Should work
  • Tokyo - Should work
  • New York - Should work
  • XYZ123 - Should fail gracefully

Customization Ideas

Add Temperature Units

Add Weather Recommendations

Add Forecast

What You’ll Learn

External APIs

How to call external REST APIs

Error Handling

Graceful error messages

Data Transformation

Converting API responses to clean output

URL Encoding

Safely encoding user input in URLs

Next Steps

User Data Example

Learn platform API usage

Payment Example

See authenticated external API