OpenAIImages

1.0.0

An unofficial Swift SDK for the new GPT Image Generation API.
m1guelpf/swift-openai-images

What's New

OpenAI Images API

Swift Version GitHub license

An unofficial Swift SDK for the new GPT Image Generation API.

Installation

Swift Package Manager

Add the following to your Package.swift:

dependencies: [
	.package(url: "https://github.com/m1guelpf/swift-openai-images.git", .branch("main"))
]
Installing through XCode
CocoaPods

Ask ChatGPT to help you migrate away from CocoaPods.

Getting started 🚀

import OpenAIImages

let client = ImagesAPI(authToken: OPENAI_API_KEY)

let response = try await client.create(prompt: "A cat playing chess")

let response = try await client.edit(
    image: .url(Bundle.main.url(forResource: "myImage", withExtension: "png"), format: .png),
    prompt: "Change the color of the sky"
)

Architecture

Creating a new instance

To interact with the Images API, create a new instance of ImagesAPI with your API key:

let client = ImagesAPI(authToken: YOUR_OPENAI_API_TOKEN)

Optionally, you can provide an Organization ID and/or project ID:

let client = ImagesAPI(
	authToken: YOUR_OPENAI_API_KEY,
	organizationId: YOUR_ORGANIZATION_ID,
	projectId: YOUR_PROJECT_ID
)

For more advanced use cases like connecting to a custom server, you can customize the URLRequest used to connect to the API:

let urlRequest = URLRequest(url: MY_CUSTOM_ENDPOINT)
urlRequest.addValue("Bearer \(YOUR_API_KEY)", forHTTPHeaderField: "Authorization")

let client = ImagesAPI(connectingTo: urlRequest)

Creating Images

To generate new images, call the create method with a CreateImageRequest instance:

let response = try await client.create(CreateImageRequest(
	prompt: "A children's book drawing of a veterinarian using a stethoscope to listen to the heartbeat of a baby otter.",
))

Alternatively, you can pass the parameters directly to the create method:

let response = try await client.create(
	prompt: "A children's book drawing of a veterinarian using a stethoscope to listen to the heartbeat of a baby otter."
    size: .landscape
)

Editing Images

To edit an existing image, call the edit method with an EditImageRequest instance:

let response = try await client.edit(EditImageRequest(
    images: [.image(name: "image.png", data: imageData, format: .png)],
    prompt: "Change the background to a beach."
)

Alternatively, you can pass the parameters directly to the edit method:

let response = try await client.edit(
    images: [.url(URL(string: "https://example.com/image.webp"), format: .webp)],
    prompt: "Change the background to a beach."
)

License

This project is licensed under the MIT License - see the LICENSE file for details.

Description

  • Swift Tools 6.0.0
View More Packages from this Author

Dependencies

  • None
Last updated: Fri May 16 2025 06:27:17 GMT-0900 (Hawaii-Aleutian Daylight Time)