---
title: Functions SDK Reference
description: Invoke serverless functions with the InsForge Swift SDK
---

import SwiftSdkInstallation from '/snippets/swift-sdk-installation.mdx';

## Installation

<SwiftSdkInstallation />

<Note>
Currently, InsForge only supports JavaScript/TypeScript functions running in a Deno environment.
</Note>

---

## invoke()

Invoke a serverless function by slug.

### Parameters

- `slug` (String) - Function slug/name
- `body` ([String: Any], optional) - Request body as dictionary

### Overloads

The `invoke` method has three overloads:

1. **With dictionary body, returning typed response**
   ```swift
   func invoke<T: Decodable>(_ slug: String, body: [String: Any]?) async throws -> T
   ```

2. **With Encodable body, returning typed response**
   ```swift
   func invoke<I: Encodable, O: Decodable>(_ slug: String, body: I) async throws -> O
   ```

3. **Without expecting response body**
   ```swift
   func invoke(_ slug: String, body: [String: Any]?) async throws
   ```

<Note>
SDK automatically includes authentication token from logged-in user.
</Note>

---

## Examples

### Example: Basic Invocation with Typed Response

```swift
// Define response model
struct HelloResponse: Codable {
    let message: String
    let timestamp: String
}

// Invoke function with dictionary body
let response: HelloResponse = try await insforge.functions.invoke(
    "hello-world",
    body: ["name": "World", "greeting": "Hello"]
)

print(response.message)  // "Hello, World!"
```

### Example: With Encodable Request Body

```swift
// Define request and response models
struct GreetingRequest: Codable {
    let name: String
    let greeting: String
}

struct GreetingResponse: Codable {
    let message: String
    let timestamp: String
}

// Invoke with typed request
let request = GreetingRequest(name: "World", greeting: "Hello")
let response: GreetingResponse = try await insforge.functions.invoke(
    "hello-world",
    body: request
)

print(response.message)
```

---

## Error Handling

```swift
do {
    let response: MyResponse = try await insforge.functions.invoke(
        "my-function",
        body: ["key": "value"]
    )
    print("Success: \(response)")
} catch let error as InsForgeError {
    switch error {
    case .httpError(let statusCode, let message):
        print("HTTP Error \(statusCode): \(message)")
    case .decodingError(let error):
        print("Failed to decode response: \(error)")
    case .networkError(let error):
        print("Network error: \(error)")
    default:
        print("Error: \(error)")
    }
} catch {
    print("Unexpected error: \(error)")
}
```
