# Headless SDK

### Customize the payment experience using Headless functions

#### 1. Initialize the Juspay Hyperswitch SDK

Initialize Hyperswitch Headless SDK onto your app with your publishable key. To get a Publishable Key please find it [here](https://app.hyperswitch.io/developers).

```swift
// pod 'hyperswitch-sdk-ios'
paymentSession = PaymentSession(publishableKey: publishableKey)
```

#### 2. Create a Payment Intent

Make a request to the endpoint on your server to create a new Payment. The `clientSecret` returned by your endpoint is used to initialize the payment session.

{% hint style="danger" %}
**Important**: Make sure to never share your API key with your client application as this could potentially compromise your security
{% endhint %}

#### 3. Initialize your Payment Session

Initialize a Payment Session by passing the clientSecret to the `initPaymentSession`

```swift
paymentSession?.initPaymentSession(paymentIntentClientSecret: paymentIntentClientSecret)
```

| options (Required)      | Description                                                     |
| ----------------------- | --------------------------------------------------------------- |
| `clientSecret (string)` | **Required.** Required to use as the identifier of the payment. |

#### 4. Craft a customized payments experience

Using the `paymentSession` object, the default customer payment method data can be fetched, using which you can craft your own payments experience. The `paymentSession` object also exposes a `confirmWithCustomerDefaultPaymentMethod` function, using which you can confirm and handle the payment session.

<pre class="language-swift"><code class="lang-swift">private var handler: PaymentSessionHandler?
 
func initSavedPaymentMethodSessionCallback(handler: PaymentSessionHandler)-> Void {
    self.handler = handler
}
    
@objc func launchHeadless(_ sender: Any) {
    paymentSession!.getCustomerSavedPaymentMethods(initSavedPaymentMethodSessionCallback)
<strong>}
</strong>
@objc func confirmPayment(_ sender: Any) {
    let paymentMethod = self.handler!.getCustomerLastUsedSavedPaymentMethodData(callback)
}
    
@objc func confirmPayment(_ sender: Any) {
    self.handler!.confirmWithLastUsedSavedPaymentMethodData(callback)
}
</code></pre>

**Payload for** `confirmWithCustomerLastUsedPaymentMethod(callback)`

| options (Required)    | Description                       |
| --------------------- | --------------------------------- |
| `callback (function)` | Callback to get confirm response. |


---

# 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.hyperswitch.io/integration-guide/payment-suite/payment-method-card/mobile/ios/headless.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.
