DEV Community

Daniel Dong
Daniel Dong

Posted on

Let AI Call Your API (Function Calling Tutorial)

Function calling lets AI decide when to call your API. Here's how to implement it in 30 lines of code — with real examples.

AI is great at generating text. But you know what's better? AI that can take actions.

Function calling lets AI decide: "I need to call this API to answer the user's question."

Here's how to implement it.

The Setup

Let's say you have a weather API. Instead of AI making up the weather, it can call your API to get real data.

Step 1: Define Your Functions

functions = [
    {
        "name": "get_weather",
        "description": "Get current weather for a city",
        "parameters": {
            "type": "object",
            "properties": {
                "city": {"type": "string", "description": "City name"}
            },
            "required": ["city"]
        }
    }
]
Enter fullscreen mode Exit fullscreen mode

Step 2: Let AI Decide

response = client.chat.completions.create(
    model="deepseek-v4-pro",
    messages=[{"role": "user", "content": "What's the weather in Tokyo?"}],
    tools=[{"type": "function", "function": f} for f in functions]
)

# AI decides to call get_weather("Tokyo")
message = response.choices[0].message
Enter fullscreen mode Exit fullscreen mode

Step 3: Execute the Function

if message.tool_calls:
    # AI wants to call a function
    func_name = message.tool_calls[0].function.name
    args = json.loads(message.tool_calls[0].function.arguments)

    if func_name == "get_weather":
        result = requests.get(f"https://api.weather.com/v1/{args['city']}").json()

        # Send result back to AI
        final_response = client.chat.completions.create(
            model="deepseek-v4-pro",
            messages=[
                {"role": "user", "content": "What's the weather in Tokyo?"},
                message,
                {"role": "tool", "tool_call_id": message.tool_calls[0].id, 
                 "content": json.dumps(result)}
            ]
        )
        print(final_response.choices[0].message.content)
Enter fullscreen mode Exit fullscreen mode

Real Example: AI Assistant with Tools

# Full example: AI that can check weather, search DB, and send emails
functions = [get_weather, query_database, send_email]

while True:
    user_input = input("You: ")

    response = client.chat.completions.create(
        model="deepseek-v4-pro",
        messages=[{"role": "user", "content": user_input}],
        tools=[{"type": "function", "function": f} for f in functions]
    )

    # AI automatically calls the right function
    # You just execute it and return the result
Enter fullscreen mode Exit fullscreen mode

Try It

  1. Get a free API key → aibridge-api.com
  2. Copy the code above
  3. Define your own functions
  4. Let AI call them

Pro tip: Use deepseek-v4-pro for function calling (best at reasoning which function to call).

mainpage

models

playground

pricing

Top comments (0)