# Restoring Purchases

Provide a **Restore Purchases** button (for example in your settings screen) so users who reinstalled your app or switched devices can regain access to their non-consumable and subscription purchases without paying again.

## Trigger a restore

```swift
@MainActor func restoreCompletedTransactions(
    completion: ((Result<Void, OnsidePaymentQueueRequestRestoreError>) -> Void)?
)
```

```swift
func restoreTapped() {
    Onside.defaultPaymentQueue().restoreCompletedTransactions { result in
        if case .failure(let error) = result {
            print("Couldn't start restore: \(error)")   // .loginDiscarded
        }
    }
}
```

{% hint style="warning" %}
The `completion` argument is **required** — pass a closure or `completion: nil`. Like `add(_:completion:)`, the completion only reports the pre-flight outcome (`OnsidePaymentQueueRequestRestoreError.loginDiscarded` if the user dismisses the login screen). Restored transactions are delivered through your observer.
{% endhint %}

## Receive restored transactions

Restored purchases are delivered to the **same** `onsidePaymentQueue(_:updatedTransactions:)` observer method, with a state of `.restored`. Handle them exactly like a purchase — unlock content and finish the transaction:

```swift
case .restored:
    unlockContent(for: transaction.payment.product.productIdentifier)
    queue.finishTransaction(transaction)
```

(See [Making a Purchase](/sdk/purchasing/making-a-purchase.md) for the full `switch`.)

## Know when the restore finishes

After all restored transactions are delivered, OnsideKit calls one of two optional observer methods. Use them to update your UI (e.g. hide a spinner):

```swift
func onsidePaymentQueueRestoreCompletedTransactionsFinished(_ queue: OnsidePaymentQueue) {
    // Restore finished successfully.
}

func onsidePaymentQueue(
    _ queue: OnsidePaymentQueue,
    restoreCompletedTransactionsFailedWithError error: OnsideTransactionsRestoreError
) {
    // Restore failed — show an error.
}
```

### Restore errors

`OnsideTransactionsRestoreError`:

<table><thead><tr><th width="220">Case</th><th>Cause</th></tr></thead><tbody><tr><td><code>.cancelled</code></td><td>The restore was cancelled.</td></tr><tr><td><code>.connectionError</code></td><td>Network failure.</td></tr><tr><td><code>.serviceUnavailable</code></td><td>Server returned 5xx.</td></tr><tr><td><code>.appNotRegistered</code></td><td>The app/install isn't recognized by Onside (HTTP 404).</td></tr><tr><td><code>.internalError</code></td><td>Parsing or other unexpected error.</td></tr></tbody></table>

See the full [Error Reference](/sdk/reference/errors.md).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.onside.io/sdk/purchasing/restoring-purchases.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
