# Android SDK

📱 Installation guide for our Android SDK

> ### 💡Supporting the latest versions
>
> The SDK supports Android 9 and above

Add the repository in your `build.gradle` or `build.gradle.kts`.

```
repositories {
    maven {
        url("https://s01.oss.sonatype.org/content/repositories/releases/")
    }
}
```

You can install the SDK by adding the following line to the `build.gradle` file under dependencies:

```
implementation("com.moonpay.sdk:MoonPaySdk-android:1.1.2")
```

Initialize the SDK in your application with the flow, environment and any parameters related to Buy, Sell, NFT or Swap. Additionally you can choose to render the widget either as an in-app browser or a webview.

> ### 💡How to choose?
>
> An in-app browser will allow you to use native features like Google Pay, social sign-in and popups.
>
> A webview will allow you to use a communication layer between the widget and your app and therefore get your app to perform certain actions once some events occur in the widget.

```
val handlers = MoonPayHandlers(
  onSwapsCustomerSetupComplete = {
    Log.i("HANDLER CALLED","onSwapsCustomerSetupComplete called!")
	},
  onAuthToken = {
    Log.i("HANDLER CALLED", "onAuthToken called with payload $it")
	},
  onLogin = {
    Log.i("HANDLER CALLED", "onLogin called with payload $it")
	},
  onInitiateDeposit = {
    Log.i("HANDLER CALLED","onInitiateDeposit called with payload $it")
    OnInitiateDepositResponsePayload(depositId = "someDepositId")
	},
  onKmsWalletCreated = {
    Log.i("HANDLER CALLED", "onKmsWalletCreated called!")
	},
  onUnsupportedRegion = {
    Log.i("HANDLER CALLED", "onUnsupportedRegion called!")
	}
)

val params = MoonPayBuyQueryParams("pk_test_123")
params.setCurrencyCode("ETH")

val config = MoonPaySdkBuyConfig(
  environment = MoonPayWidgetEnvironment.Sandbox,
  debug = true,
  params = params,
  handlers = handlers
)

// Initialize the SDK using 'this' reference for MainActivity
val moonPaySdk = MoonPayAndroidSdk(config = config, activity = this)

moonPaySdk.show(MoonPayRenderingOptionAndroid.WebViewOverlay())
```

```
val handlers = MoonPayHandlers(
  onSwapsCustomerSetupComplete = {
    Log.i("HANDLER CALLED","onSwapsCustomerSetupComplete called!")
	},
  onAuthToken = {
    Log.i("HANDLER CALLED", "onAuthToken called with payload $it")
	},
  onLogin = {
    Log.i("HANDLER CALLED", "onLogin called with payload $it")
	},
  onInitiateDeposit = {
    Log.i("HANDLER CALLED","onInitiateDeposit called with payload $it")
    OnInitiateDepositResponsePayload(depositId = "someDepositId")
	},
  onKmsWalletCreated = {
    Log.i("HANDLER CALLED", "onKmsWalletCreated called!")
	},
  onUnsupportedRegion = {
    Log.i("HANDLER CALLED", "onUnsupportedRegion called!")
	}
)

val params = MoonPayBuyQueryParams("pk_test_123")
params.setCurrencyCode("ETH")

val config = MoonPaySdkBuyConfig(
  environment = MoonPayWidgetEnvironment.Sandbox,
  debug = true,
  params = params,
  handlers = handlers
)

// Initialize the SDK using 'this' reference for MainActivity
val moonPaySdk = MoonPayAndroidSdk(activity = this)

moonPaySdk.updateConfig(config)

moonPaySdk.show(MoonPayRenderingOptionAndroid.WebViewOverlay())
```

See the parameters pages for Buy, Sell and Swap for details on which parameters can be passed for each flow.

Arguments:

* `config`: **MoonPaySdkConfig**,

Your config variant must match your query params variant.

Variants:

* `MoonPaySdkBuyConfig`
* `MoonPaySdkSellConfig`
* `MoonPaySdkSwapsCustomerSetupConfig`
* `MoonPaySdkLegacyConfig`

Arguments:

* `debug`: **Boolean**
* `environment`: **MoonPayWidgetEnvironment**
* `params`: **MoonPayQueryParams**
* `handlers`: **MoonPayHandlers?**

> ### 📘Configuration
>
> Possible values for `environment`: MoonPayWidgetEnvironment.sandbox, MoonPayWidgetEnvironment.production

Your query params variant must match your config variant.

Variants:

* `MoonPayBuyQueryParams`
* `MoonPaySellQueryParams`
* `MoonPaySwapsCustomerSetupParams`
* `MoonPayLegacyParams`

All of the following are optional. However, you must explicitly pass `nil` for each handler you don't need to implement.

Arguments:

* `onSwapsCustomerSetupComplete`: **() -> Void**
* `onUnsupportedRegion`: **() -> Void**
* `onKmsWalletCreated`: **() -> Void**
* `onAuthToken`: **(OnAuthTokenRequestPayload) -> Void**
* `onLogin`: **(OnLoginRequestPayload) -> Void**
* `onInitiateDeposit`: **(OnInitiateDepositRequestPayload) -> OnInitiateDepositResponsePayload**

Arguments:

* `token`: **String**
* `csrfToken`: **String**

Arguments:

* `isRefresh`: **Boolean**

Arguments:

* `transactionId`: **String**
* `cryptoCurrencyAmount`: **String**
* `cryptoCurrencyAmountSmallestDenomination`: **String**
* `fiatCurrencyAmount`: **String?**
* `depositWalletAddress`: **String**
* `cryptoCurrency`: **CryptoCurrency**
* `fiatCurrency`: **FiatCurrency**

Arguments:

* `depositId`: **String**

Arguments:

* `id`: **String**
* `name`: **String**
* `code`: **String**
* `contractAddress`: **String?**
* `chainId`: **String?**
* `coinType`: **String?**
* `networkCode`: **String?**

Arguments:

* `id`: **String**
* `name`: **String**
* `code`: **String**

If you're using the `walletAddress` or `walletAddresses` query param, you need to sign your widget URL before you can display the widget. Learn more about URL signing.

`moonPaySdk.show` takes one argument, `mode`, which takes a value of type **MoonPayRenderingOptionAndroid**. The mode variants are **MoonPayRenderingOptionAndroid.WebViewOverlay** and **MoonPayRenderingOptionAndroid.InAppBrowser**.

You need to sign your widget URL before you can display the widget. Learn more about URL signing.

```
// Initialise the SDK as above

val urlSignature = moonpay.generateUrlForSignature();
val jsonObj = JSONObj.getJSONObject(urlSignature);

// The URL for signature should be sent to your backend, which should then
// sign it with your API secret and return the signature.
val response = { make a post request to '/sign-url' with the jsonObj }

// Once you have the signature, you can update the SDK with it and show the
// widget.
moonpay.updateSignature(response)
```

Updated 3 months ago

***

* [Table of Contents](broken://pages/S8x8Jzhdwzfe6tzsdWJg)
* * [Install](broken://pages/S8x8Jzhdwzfe6tzsdWJg)
  * [Initialize](broken://pages/S8x8Jzhdwzfe6tzsdWJg)
  * [SDK Configuration](broken://pages/S8x8Jzhdwzfe6tzsdWJg)
    * [MoonPayAndroidSdk](broken://pages/S8x8Jzhdwzfe6tzsdWJg)
    * [MoonPaySdkConfig](broken://pages/S8x8Jzhdwzfe6tzsdWJg)
    * [MoonPayQueryParams](broken://pages/S8x8Jzhdwzfe6tzsdWJg)
    * [MoonPayHandlers](broken://pages/S8x8Jzhdwzfe6tzsdWJg)
  * [Handler Payload Types](broken://pages/S8x8Jzhdwzfe6tzsdWJg)
    * [OnAuthTokenRequestPayload](broken://pages/S8x8Jzhdwzfe6tzsdWJg)
    * [OnLoginRequestPayload](broken://pages/S8x8Jzhdwzfe6tzsdWJg)
    * [OnInitiateDepositRequestPayload](broken://pages/S8x8Jzhdwzfe6tzsdWJg)
    * [OnInitiateDepositResponsePayload](broken://pages/S8x8Jzhdwzfe6tzsdWJg)
    * [CryptoCurrency](broken://pages/S8x8Jzhdwzfe6tzsdWJg)
    * [FiatCurrency](broken://pages/S8x8Jzhdwzfe6tzsdWJg)
  * [Display](broken://pages/S8x8Jzhdwzfe6tzsdWJg)


---

# 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://gitbook.keytrust.one/swap/swap-api-driven-flow/integrations-sdks/android-sdk.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.
