# Turso Quickstart (Swift)

> Get started with Turso and Swift using the libSQL client in a few simple steps.
> Canonical HTML: https://tursodb.cn/docs/sdk/swift/quickstart/
> Upstream Source (MIT): https://docs.turso.tech/sdk/swift/quickstart
> Language: en

---
<div class="doc-callout doc-callout-warning"><div class="doc-callout-title">WARNING</div><div class="doc-callout-body">

This is currently in technical preview. [Join us in Discord](https://tur.so/discord) to report any issues.

</div></div>

In this Swift quickstart we will learn how to:

* Retrieve database credentials
* Install the libSQL package
* Connect to a local or remote Turso database
* Execute a query using SQL
* Sync changes to local database (optional)

**1. Retrieve database credentials**

You will need an existing database to continue. If you don't have one, [create one](/docs/quickstart/).

Get the database URL:

```bash
turso db show --url <database-name>
```

Get the database authentication token:

```bash
turso db tokens create <database-name>
```

Assign credentials to the environment variables inside `.env`.

```bash
TURSO_DATABASE_URL=
TURSO_AUTH_TOKEN=
```

<div class="doc-callout doc-callout-info"><div class="doc-callout-title">INFO</div><div class="doc-callout-body">

You will want to make sure to handle database tokens securely.

</div></div>

**2. Install**

First begin by adding `libsql` as a package dependency in XCode using this repo:

<div class="doc-cards">
<a class="doc-card" href="https://github.com/tursodatabase/libsql-swift" target="_blank" rel="noopener noreferrer"><div class="doc-card-title"><span>libSQL Swift</span><span class="doc-card-arrow" aria-hidden="true">↗</span></div><p class="doc-card-desc">Build from source code</p></a>
</div>


Or add it to your SwiftPM dependencies:

```swift
import PackageDescription

let package = Package(
    // ...
    dependencies: [
        .package(url: "https://github.com/tursodatabase/libsql-swift", from: "0.1.1"),
    ],
    // ...
)
```

**3. Connect**

You must first create a `Database` object and then open a `Connection` to it:

<details class="doc-accordion"><summary>Embedded Replicas</summary><div class="doc-accordion-body">

```swift
import Libsql

let db = try Database(
    path: "./local.db",
    url: "TURSO_DATABASE_URL",
    authToken: "TURSO_AUTH_TOKEN",
    syncInterval: 300
)

let conn = try db.connect()
```

</div></details>


<details class="doc-accordion"><summary>Local only</summary><div class="doc-accordion-body">

```swift
import Libsql

let db = try Database(
    path: "./local.db",
)

let conn = try db.connect()
```

</div></details>


<details class="doc-accordion"><summary>Remote only</summary><div class="doc-accordion-body">

```swift
import Libsql

let db = try Database(
    url: "TURSO_DATABASE_URL",
    authToken: "TURSO_AUTH_TOKEN"
)

let conn = try db.connect()
```

</div></details>


**4. Execute**

You can execute a SQL query against your existing database by calling `query()`,
or `execute()` when you expect the query to not yield any rows:

```swift
let rows = try conn.query("SELECT * FROM users")
```

If you need to use placeholders for values, you can do that:

```swift Positional Query
try conn.query("SELECT * FROM users WHERE id = ?", 1)
```

```swift Positional Insert
try conn.execute("INSERT INTO users (id) VALUES (?)", 1)
```

<div class="doc-callout doc-callout-info"><div class="doc-callout-title">NOTE</div><div class="doc-callout-body">

Named arguments are not supported yet.

</div></div>


**5. Sync (Embedded Replicas only)**

When using embedded replicas you should call `sync()` on the database type to
sync your local database with the primary database, unless you are using
`sync_interval` (though there is no issue with calling `sync` with
`sync_interval` enabled):

```swift
try db.sync()
```
