Swift Examples - Stability AI

Service setup

Create a Stability AI service in the AIProxy dashboard

Follow the integration guide, selecting the Stability AI icon on the 'Create a New Service' form.

How to generate an image with Stability.ai

In the snippet below, replace NSImage with UIImage if you are building on iOS. For a SwiftUI example, see this gist.

import AIProxy

/* Uncomment for BYOK use cases */
// let stabilityService = AIProxy.stabilityAIDirectService(
//     unprotectedAPIKey: "your-stability-key"
// )

/* Uncomment for all other production use cases */
// let service = AIProxy.stabilityAIService(
//     partialKey: "partial-key-from-your-developer-dashboard",
//     serviceURL: "service-url-from-your-developer-dashboard"
// )

do {
    let body = StabilityAIUltraRequestBody(prompt: "Lighthouse on a cliff overlooking the ocean")
    let response = try await stabilityService.ultraRequest(body: body)
    let image = NSImage(data: response.imageData)
    // Do something with `image`
}  catch AIProxyError.unsuccessfulRequest(let statusCode, let responseBody) {
    print("Received \(statusCode) status code with response body: \(responseBody)")
} catch {
    print("Could not generate an image with StabilityAI: \(error.localizedDescription)")
}