---
title: "Swift"
description: "Official CarsXE SDK for Swift."
canonical_url: "https://docs.carsxe.com/docs/sdks/swift"
markdown_url: "https://docs.carsxe.com/docs/sdks/swift.md"
last_updated: "1980-01-01"
x_farming_labs_generated_preamble: true
agent:
  task: "Install and call the CarsXE Swift SDK for vehicle-data endpoints."
  outcome: "The Swift client is installed and its documented example returns a successful CarsXE response."
  prerequisites:
    - "A valid CarsXE API key."
    - "The runtime and package tooling required by the Swift installation steps."
  verification:
    - "Run the documented Swift example and confirm it authenticates and returns vehicle data without an SDK or API error."
  failureModes:
    - symptom: "The Swift client cannot install, authenticate, or complete the example request."
      resolution: "Verify the runtime version, package installation, API-key environment variable, and endpoint inputs before rerunning the example."
---

# Swift
URL: /docs/sdks/swift
LLM index: /llms.txt
Description: Official CarsXE SDK for Swift.
Related: /docs/sdks, /docs/get-started

<!-- farming-labs:agent-contract:start -->
## Agent Contract

Task: Install and call the CarsXE Swift SDK for vehicle-data endpoints.
Outcome: The Swift client is installed and its documented example returns a successful CarsXE response.

### Prerequisites

- A valid CarsXE API key.
- The runtime and package tooling required by the Swift installation steps.

### Verification

- Run the documented Swift example and confirm it authenticates and returns vehicle data without an SDK or API error.

### Failure Modes

- The Swift client cannot install, authenticate, or complete the example request. — Recovery: Verify the runtime version, package installation, API-key environment variable, and endpoint inputs before rerunning the example.
<!-- farming-labs:agent-contract:end -->

Task: Install and call the CarsXE Swift SDK for vehicle-data endpoints.
Related: /docs/sdks/swift, /docs/get-started, /docs/sdks

The `carsxe` package is the official CarsXE client for Swift. It wraps the REST API with idiomatic methods and handles authentication. Requires Swift 5.5 or later.

## Installation

<CodeGroup title="Add the package">

```swift {{ title: "Package.swift" }}
dependencies: [
    .package(url: "https://github.com/carsxe/carsxe-swift-package", from: "1.0.0"),
],
targets: [
    .target(
        name: "YourTarget",
        dependencies: ["carsxe"]
    ),
]
```

```text {{ title: "Xcode" }}
File → Add Package Dependencies
https://github.com/carsxe/carsxe-swift-package
```

</CodeGroup>

## Quick start

```swift
import carsxe

let carsxe = CarsXE(apiKey: ProcessInfo.processInfo.environment["CARSXE_API_KEY"]!)

do {
    let vehicle = try carsxe.specs(["vin": "WBAFR7C57CC811956"])
    print(vehicle)
} catch {
    print("Error: \(error)")
}
```

## Methods

### Core

| Method | Description |
|---|---|
| `specs(_:)` | Decode a 17-character VIN into full vehicle specifications |
| `marketValue(_:)` | Retail, trade-in, and auction value estimates |
| `recalls(_:)` | Open safety recalls by VIN, make, model, or year |
| `plateDecoder(_:)` | VIN lookup from a license plate (50+ countries) |
| `history(_:)` | Title events, salvage, junk, and insurance records |

### Bulk recall batch

| Method | Description |
|---|---|
| `submitBulkRecallBatch(_:)` | Submit a batch of VINs for recall lookup |
| `getBulkRecallBatchStatus(_:)` | Poll job status |
| `getBulkRecallBatchResults(_:)` | Retrieve completed results |
| `getBulkRecallBatchDownloadUrl(_:)` | Get a CSV download URL |

## Endpoint examples

Runnable examples for each method. All of them assume the `carsxe` instance from [Quick start](#quick-start) and are shown without the surrounding `do/catch` for brevity — every call is `try`.

### International VIN Decoder

```swift
let intvin = try carsxe.intVinDecoder(["vin": "WF0MXXGBWM8R43240"])
print(intvin)
```

### Market Value

```swift
let marketvalue = try carsxe.marketValue(["vin": "WBAFR7C57CC811956"])
print(marketvalue)
```

### Vehicle History

```swift
let history = try carsxe.history(["vin": "WBAFR7C57CC811956"])
print(history)
```

### License Plate Decoder

```swift
let decodedPlate = try carsxe.plateDecoder([
    "plate": "7XER187",
    "state": "CA",
    "country": "US",
])
print(decodedPlate)
```

### Vehicle Images

```swift
let images = try carsxe.images([
    "make": "BMW",
    "model": "X5",
    "year": "2019",
])
print(images)
```

### Vehicle Recalls

```swift
let recalls = try carsxe.recalls(["vin": "1C4JJXR64PW696340"])
print(recalls)
```

### Plate Image Recognition

Takes a labelled `imageUrl` argument rather than a dictionary.

```swift
let plateResult = try carsxe.plateImageRecognition(
    imageUrl: "https://imagedelivery.net/moyiiSImjJPI_EZVxNMBBw/f49aed53-d736-4370-f3f4-97418841c800/public"
)
print(plateResult)
```

### VIN OCR from Image

```swift
let vinocr = try carsxe.vinOCR(
    imageUrl: "https://imagedelivery.net/moyiiSImjJPI_EZVxNMBBw/f49aed53-d736-4370-f3f4-97418841c800/public"
)
print(vinocr)
```

### Year-Make-Model Search

```swift
let yymm = try carsxe.yearMakeModel([
    "year": "2012",
    "make": "BMW",
    "model": "5 Series",
])
print(yymm)
```

### OBD Code Decoder

```swift
let obdcode = try carsxe.obdCodesDecoder(["code": "P0115"])
print(obdcode)
```

### Lien and Theft

```swift
let lienTheft = try carsxe.lienAndTheft(["vin": "2C3CDXFG1FH762860"])
print(lienTheft)
```

## Error handling

Methods `throw` on API errors. Use `do/catch`:

```swift
do {
    let vehicle = try carsxe.specs(["vin": "WBAFR7C57CC811956"])
    print(vehicle)
} catch {
    print("CarsXE error: \(error)")
}
```

## Best practices

**Read the key from the environment or Keychain.** For iOS/macOS apps, store the key in the Keychain rather than environment variables for production use.

**Create one instance and reuse it** — don't instantiate the client per request.

**Store your key in an environment variable** for development:

```bash
# .env or Xcode scheme environment variables
CARSXE_API_KEY=your_key_here
```

## Sitemap

See the full [sitemap](/sitemap.md) for all pages.
Docs-scoped sitemap: [/docs/sitemap.md](/docs/sitemap.md).
Well-known sitemap: [/.well-known/sitemap.md](/.well-known/sitemap.md).
