Getting Started with FreeToken Swift
This is a quick guide to get you up and running with the FreeToken Swift SDK. Follow these steps to integrate the SDK and perform your first AI run!
Installation
Add FreeToken to your project using Swift Package Manager:
- Open your project in Xcode
- Go to
File>Add Package Dependencies... - Enter the repository URL in the top right "Search" box:
https://github.com/FreeTokenAI/FreeTokenSwiftMake sure you are logged into your GitHub account in Xcode if you encounter any issues - Enter the version you want to use (e.g.,
1.0.0ormainfor the latest) - Click
Add Package - Select the target you want to add the package to and click
Add Package. The target is usually the name of your app.
Quick Start
Creating your App & Agent in the FreeToken web console
- Go to the FreeToken Console
Sign upfor a new account orlog inif you already have one- Go to the Apps navigation item and click
Create New App. - Fill in the required details and create your app
- In the
Manage App Tokenssection, create a newApp Token. Save this token as you'll need it to configure the SDK - Create a new Agent in the
Agentssection by selectingCreate Agent - Fill in the required details and create your Agent
You are now ready to configure your app!
1. Configure the Client
import FreeToken
// Configure with your API token
let client = FreeToken.shared.configure(
appToken: "your-api-token-here"
)
2. Register Your Device
Before using AI features, register your device with a scope:
await client.registerDeviceSession(scope: "my-app-v1") {
print("Device registered successfully!")
// Device is ready for AI operations
} error: { error in
print("Failed to register device: \(error.localizedDescription)")
}
3. Download AI Model
If you want to use local AI processing:
await client.downloadAIModel { state in
switch state {
case .downloaded:
print("AI model ready for local processing")
case .aiNotSupported:
print("Device doesn't support local AI - will use cloud")
case .cloudOnly:
print("Model is cloud-only and cannot be downloaded")
}
} error: { error in
print("Failed to download model: \(error.localizedDescription)")
}
4. Create Your First Message Thread
// Create a message thread for conversation
await client.createMessageThread { messageThread in
print("Created message thread: \(messageThread.id)")
// Add a user message
let userMessage = Message(role: .user, content: "Hello! Can you help me understand Swift closures?")
// Add message to the thread
await client.addMessageToThread(id: messageThread.id, message: userMessage) { message in
print("Added message: \(message.content)")
} error: { error in
print("Failed to add message: \(error.localizedDescription)")
}
} error: { error in
print("Failed to create thread: \(error.localizedDescription)")
}
5. Run the Message Thread
// Run the thread through AI
await client.runMessageThread(id: messageThreadID) { responseMessage in
print("AI Response: \(responseMessage.content)")
} error: { error in
print("AI processing failed: \(error.localizedDescription)")
} chatStatusStream: { token, status in
// Returns enum representing the internal state of the AI model and streaming response (if available)
print("Chat status: \(status) (token: \(token))")
}