# Swift

## Core SDK - Swift

The XpansionChain Core SDK provides convenient access to XpansionChain's APIs and smart contracts to help projects build better web3 games and marketplaces.

💡ROLLUPS THIS SDK SUPPORTS

* XpansionChain

CONTENTS

* <mark style="color:blue;">Installation</mark>
* <mark style="color:blue;">Initialization</mark>
* <mark style="color:blue;">Standard API requests</mark>
* <mark style="color:blue;">SDK functions</mark>
* <mark style="color:blue;">Wallet connection</mark>
* <mark style="color:blue;">Autogenerated code</mark>
* <mark style="color:blue;">Further documentation</mark>

***

📚SDK LINKS

* <mark style="color:blue;">SDK reference</mark>
* <mark style="color:blue;">Github repository</mark>

### Installation[​](https://docs.x.immutable.com/docs/sdks/core/swift#installation) <a href="#installation" id="installation"></a>

#### Prerequisites[​](https://docs.x.immutable.com/docs/sdks/core/swift#prerequisites) <a href="#prerequisites" id="prerequisites"></a>

* iOS 13.0 or macOS 10.15
* Swift 5.7

#### Swift package manager[​](https://docs.x.immutable.com/docs/sdks/core/swift#swift-package-manager) <a href="#swift-package-manager" id="swift-package-manager"></a>

In your `Package.swift`:

```
dependencies: [
    .package(url: "https://github.com/XpansionChain/imx-core-sdk-swift.git", from: "1.0.0-beta.1")
]
```

#### Cocoapods[​](https://docs.x.immutable.com/docs/sdks/core/swift#cocoapods) <a href="#cocoapods" id="cocoapods"></a>

In your `Podfile`:

```
platform :ios, '13.0'
use_frameworks!

target 'MyApp' do
  pod 'XpansionChainCore'
end
```

### Initialization[​](https://docs.x.immutable.com/docs/sdks/core/swift#initialization) <a href="#initialization" id="initialization"></a>

The Core SDK must be initialised before any of its classes are used. Upon initialisation the base environment and log level may be defined. Once initialised a shared instace will be available for accessing the <mark style="color:blue;">workflow functions.</mark>

For example, you initialise the SDK and retrieve a URL to buy crypto through MoonPay:

```
XpansionChain.initialize(base: .sandbox)

let url = try await XpansionChain. shared.buyCryptoURL(signer: signer)
```

### Standard API requests[​](https://docs.x.immutable.com/docs/sdks/core/swift#standard-api-requests) <a href="#standard-api-requests" id="standard-api-requests"></a>

The Core SDK includes classes that interact with the XpansionChain APIs.

e.g. Get a list of collections ordered by name in ascending order

```
let collections = try await CollectionsAPI.listCollections(
    pageSize: 20,
    orderBy: .name,
    direction: "asc"
)
```

View the <mark style="color:blue;">OpenAPI spec</mark> for a full list of API requests available in the Core SDK.

NOTE: Closure based APIs are also available.

### SDK functions[​](https://docs.x.immutable.com/docs/sdks/core/swift#sdk-functions) <a href="#sdk-functions" id="sdk-functions"></a>

#### Workflows[​](https://docs.x.immutable.com/docs/sdks/core/swift#workflows) <a href="#workflows" id="workflows"></a>

These are utility functions accessed via XpansionChain`.shared` that will chain necessary API calls to complete a process or perform a transaction, including:

* Register a user with XpansionChain
* Buy cryptocurrency via MoonPay
* Buy ERC721
* Sell ERC721
* Cancel order
* Transfer ERC20/ERC721/ETH

### Wallet connection[​](https://docs.x.immutable.com/docs/sdks/core/swift#wallet-connection) <a href="#wallet-connection" id="wallet-connection"></a>

In order to use any workflow functions, you will need to pass in the connected wallet provider. This means you will need to implement your own Wallet L1 <mark style="color:blue;">Signer</mark> and L2 <mark style="color:blue;">StarkSigner.</mark>

Once you have a `Signer` instance you can generate the user's STARK key pair and use the result to instantiate a `StarkSigner`, for example, by using the default `StandardStarkSigner` provided by the SDK.

```
let keyPair = try await StarkKey.generateLegacyKeyPair(from: signer)
let starkSigner = StandardStarkSigner(pair: keyPair)
```

### Autogenerated code[​](https://docs.x.immutable.com/docs/sdks/core/swift#autogenerated-code) <a href="#autogenerated-code" id="autogenerated-code"></a>

#### SDK[​](https://docs.x.immutable.com/docs/sdks/core/swift#sdk) <a href="#sdk" id="sdk"></a>

Parts of the Core SDK are automagically generated.

#### API autogenerated code[​](https://docs.x.immutable.com/docs/sdks/core/swift#api-autogenerated-code) <a href="#api-autogenerated-code" id="api-autogenerated-code"></a>

We use OpenAPI (formally known as Swagger) to auto-generate the API clients that connect to the public APIs.

The OpenAPI spec is retrieved from <mark style="color:blue;"><https://api.x.XpansionChain.com/openapi></mark> and also saved in the repo <mark style="color:blue;">here</mark>.

Upon updating the `openapi.json` file, ensure <mark style="color:blue;">openapi-generator</mark> is installed, then run `./generateapi.sh` to regenerate the files. Any custom templates should be appropriately modified or removed as needed. These can be found in the `.openapi-generator/templates` directory.

### Further documentation[​](https://docs.x.immutable.com/docs/sdks/core/swift#further-documentation) <a href="#further-documentation" id="further-documentation"></a>

* See the <mark style="color:blue;">Developer homepage</mark> for general information on building on XpansionChain.
* Build on XpansionChain zkEVM:
  * <mark style="color:blue;">Documentation</mark>
  * <mark style="color:blue;">API reference</mark>
  * <mark style="color:blue;">Support</mark>
* Build on XpansionChain:
  * <mark style="color:blue;">Documentation</mark>
  * <mark style="color:blue;">API reference</mark>
  * <mark style="color:blue;">Support</mark>


---

# 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://xpansionchain-1.gitbook.io/xpansionchain/sdks/core-sdk/swift.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.
