Integrating External APIs with Agentic AI: A Practical Approach

작성자

카테고리:

← 피드로
DEV Community · Muhammad Zulqarnain · 2026-07-23 개발(SW)

The Power of API Integration

An agent without APIs is limited. Connected to APIs, it’s unstoppable.

Common APIs for Agents

  • Data: Stripe, Shopify, Salesforce
  • Communication: Slack, Gmail, Twilio
  • Weather: OpenWeather, WeatherAPI
  • Research: Google Search, Wikipedia
  • Analytics: Google Analytics, Amplitude

Creating API Tools

import requests

def get_weather(city: str) -> str:
    """Get current weather for a city"""
    response = requests.get(
        f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}"
    )
    data = response.json()
    return f"{data['main']['temp']}°C in {city}"

from langchain.tools import tool

@tooldef weather_tool(city: str) -> str:
    """Get weather for a city"""
    return get_weather(city)

Enter fullscreen mode Exit fullscreen mode

Error Handling

def safe_api_call(func, *args, **kwargs):
    try:
        return func(*args, **kwargs)
    except TimeoutError:
        return "API timeout - try again"
    except requests.exceptions.ConnectionError:
        return "Connection failed - check internet"
    except Exception as e:
        return f"Error: {str(e)}"

Enter fullscreen mode Exit fullscreen mode

Rate Limiting

from functools import wraps
import time

def rate_limit(calls_per_second: float):
    min_interval = 1.0 / calls_per_second
    last_called = [0.0]

    def decorator(func):
        @wraps(func)        def wrapper(*args, **kwargs):
            elapsed = time.time() - last_called[0]
            if elapsed < min_interval:
                time.sleep(min_interval - elapsed)
            result = func(*args, **kwargs)
            last_called[0] = time.time()
            return result
        return wrapper
    return decorator

Enter fullscreen mode Exit fullscreen mode

Real-World Integration

Connecting to Slack:

from slack_sdk import WebClient

client = WebClient(token=SLACK_TOKEN)

@tooldef send_slack_message(channel: str, text: str) -> str:
    """Send message to Slack channel"""
    response = client.chat_postMessage(
        channel=channel,
        text=text
    )
    return f"Message sent to {channel}"

Enter fullscreen mode Exit fullscreen mode

Best Practices

✅ Secure API keys in environment variables
✅ Implement timeout handling
✅ Log all API calls
✅ Cache responses when possible
✅ Test with mock APIs first
✅ Monitor API usage & costs
✅ Handle rate limiting gracefully

The Future

Agents that integrate 10+ APIs will be standard in 2026.

What APIs are you connecting to your agents?

원문에서 계속 ↗

코멘트

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다