---
page_title: 3. Initialize SDK
product: EC Headless
platform: Flutter
page_source: https://juspay.io/in/docs/ec-headless/flutter/base-sdk-integration/initiating-sdk
llms_txt: https://juspay.io/in/docs/llms.txt
product_llms_txt: https://juspay.io/in/docs/ec-headless/llms.txt
---


## 3. Initiating the SDK



To initialise the SDK, client needs to call the `initiate` SDK API. The initiate api call boots up Express Checkout SDK and makes it ready for all other operations

Follow the below steps to make an initiate SDK call

> **Warning**
> Initiate is to be called in the onCreate() method of your application's main activity (Home screen). Not following this will lead to increased latency.




### Step 3.1. Import HyperSDK


Import the HyperSDK namespace to get access to HyperServices class in your code

> **Error**
> Make sure that the MainActivity in Android inherits FlutterFragmentActivity instead of FlutterActivity. `android/app/src/main/kotlin/com/example/your_app_name/MainActivity.kt`





#### Code Snippets: -

#### Flutter Code Snippet:

```flutter
import 'package:hypersdkflutter/hypersdkflutter.dart';
```



### Step 3.2. Create an instance of HyperServices


Express Checkout SDK exposes the `HyperServices` class. Create an object of this class for all upcoming operations



#### Code Snippets: -

#### Flutter Code Snippet:

```flutter
// Importing Hyper SDK
// block:start:import-hyper-sdk

import 'package:hypersdkflutter/hypersdkflutter.dart';
// block:end:import-hyper-sdk

....

class MyApp extends StatelessWidget {
  // Create Juspay Object
  // // block:start:create-hyper-sdk-instance

  final hyperSDK = HyperSDK();
  // // block:end:create-hyper-sdk-instance
  ....
}

```



### Step 3.3. Create Initiate payload


Initiate API takes two parameters as input. One of the parameter is a JSON object referred as `InitiatePayload`. This payload contains certain key value pairs used by the SDK to perform a successful initiate

Refer to the following table for information about the description and sample payload.


### Payload
- **RequestId**:
  - Description: Unique uuid-v4 string
  - Value: $requestId
  - Tags: String, Mandatory
- **Service**:
  - Description: Value: in.juspay.hyperapi
  - Tags: String, Mandatory
- **Payload**:
  - Description: Parameters required to call Hyper SDK API
  - Value:
    - **Action**:
      - Description: Value: initiate
      - Tags: String, Mandatory
    - **MerchantId**:
      - Description: Unique merchant id shared during onboarding
      - Tags: String, Mandatory
    - **ClientId**:
      - Description: Unique Client id shared during onboarding
      - Tags: String, Mandatory
    - **XRoutingId**:
      - Description: Juspay recommends passing the customer's unique identifier (`customerId`) as the value of this parameter.
      - Tags: String, Mandatory
    - **Environment**:
      - Description: Environment to be used for the session. Accepted value is production
      - Tags: String, Mandatory
  - Tags: JSON, Mandatory




#### Code Snippets: -

#### Flutter Code Snippet:

```flutter
var initiatePayload = {
      "requestId": const Uuid().v4(),
      "service": "in.juspay.hyperapi",
      "payload": {
        "action": "initiate",
        "merchantId": "<merchant-id>",
        "clientId": "<client-id>",
        "xRoutingId": "<x-routing-id>",
        "environment": "production"
      }
    };
```



### Step 3.4. Create CallbackHandler


During its lifecycle, SDK emits multiple events to communicate about the transaction status.

This callback handler is passed as the second argument to the initiate API call.



#### Code Snippets: -

#### Flutter Code Snippet:

```flutter
void initiateCallbackHandler(MethodCall methodCall) {
  if (methodCall.method == "initiate_result") {
    // check initiate result
  }
}
```



### Step 3.5. Call initiate


The final step is to call the `Initiate SDK API`.

The initiate method takes two parameters: `InitiatePayload` and `HyperPaymentsCallbackAdapter`. Use the functions created in the above steps to create the parameters

> **Warning**
> Initiate is a fire-and-forget call. For every HyperService instance you should **call initiate only once.** 





#### Code Snippets: -

#### Flutter Code Snippet:

```flutter
await widget.hyperSDK.initiate(initiatePayload, initiateCallbackHandler);
```


---

## Complete Code Reference

The following code files are referenced in the steps above:

### main.dart

```
// Importing Hyper SDK
// block:start:import-hyper-sdk

import 'package:hypersdkflutter/hypersdkflutter.dart';
// block:end:import-hyper-sdk

....

class MyApp extends StatelessWidget {
  // Create Juspay Object
  // // block:start:create-hyper-sdk-instance

  final hyperSDK = HyperSDK();
  // // block:end:create-hyper-sdk-instance
  ....
}

```

### home.dart

```
void initiateHyperSDK() async {
  // Check whether hyperSDK is already initialised
  if (!await widget.hyperSDK.isInitialised()) {
    // Getting initiate payload
    // block:start:get-initiate-payload
    
    var initiatePayload = {
      "requestId": const Uuid().v4(),
      "service": "in.juspay.hyperapi",
      "payload": {
        "action": "initiate",
        "merchantId": "<merchant-id>",
        "clientId": "<client-id>",
        "xRoutingId": "<x-routing-id>",
        "environment": "production"
      }
    };
    // block:end:get-initiate-payload

    // Calling initiate on hyperSDK instance to boot up payment engine.
    // block:start:initiate-sdk

    await widget.hyperSDK.initiate(initiatePayload, initiateCallbackHandler);
    // block:end:initiate-sdk
  }
}

// Define handler for inititate callback
// block:start:initiate-callback-handler

void initiateCallbackHandler(MethodCall methodCall) {
  if (methodCall.method == "initiate_result") {
    // check initiate result
  }
}
// block:end:initiate-callback-handler



```


---

## See Also

- [2. Creating a Customer](https://juspay.io/in/docs/ec-headless/flutter/base-sdk-integration/creating-a-customer)
- [4. Rendering the Checkout Screen](https://juspay.io/in/docs/ec-headless/flutter/base-sdk-integration/rendering-the-checkout-screen)
