I have searched the documentation and i couldn’t find a tutorial for Swift. Can anybody help? There are apps on appstore but how did they use this API? What are the endpoints and request URL?
The tutorial for any language is simply the API documentation. If you pick “CURL” it will show you how you need to send a basic JSON body as https POST.
Send the string to the correct URL with the authentication header including Bearer (your-api-key). Then seeing success with what you manually created, you can replace that JSON string with dynamically-created elements - such as user input messages and past messages. Then start building.
This should just “click” for you…
POST https://api.openai.com/v1/chat/completions
Headers:
Host: api.openai.com
Content-Type: application/json
Authorization: Bearer sk-proj-123431234.
Body:
{
"model": "gpt-4o",
"messages": [
{
"role": "system",
"content": "You are a programmer's assistant."
},
{
"role": "user",
"content": "Help me write Swift-based API code using my examples."
}
]
}
thank you very much let me try
What is wrong with this implementation?
struct Message: Codable {
let role: String
let content: String
}
struct OpenAIRequest: Codable {
let model: String
let messages: [Message]
}
struct OpenAIResponse: Codable {
let messages: [Message]
}
class OpenAIService {
private let apiKey = "YOUR_API_KEY"
func fetchResponse(prompt: String, completion: @escaping (String?) -> Void) {
guard let url = URL(string: "https://api.openai.com/v1/chat/completions") else {
print("Invalid URL")
completion(nil)
return
}
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
let requestBody = OpenAIRequest(
model: "gpt-4",
messages: [
Message(role: "system", content: "You are a programmer's assistant."),
Message(role: "user", content: prompt)
]
)
do {
request.httpBody = try JSONEncoder().encode(requestBody)
} catch {
print("Failed to encode request body: \(error)")
completion(nil)
return
}
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error {
print("Error making request: \(error)")
completion(nil)
return
}
guard let data = data else {
print("No data received")
completion(nil)
return
}
// Log the raw response data
if let jsonString = String(data: data, encoding: .utf8) {
print("Raw response: \(jsonString)")
}
do {
let decoder = JSONDecoder()
let openAIResponse = try decoder.decode(OpenAIResponse.self, from: data)
let content = openAIResponse.messages.last?.content
completion(content)
} catch {
print("Failed to decode response: \(error)")
completion(nil)
}
}
task.resume()
}
}