Skip to main content
Portal’s Android SDK lets your users buy, sell and transfer crypto through Meld’s network of ramp providers, such as Transak, with the portal.ramps.meld API. Each method calls Portal’s Meld integration on the Portal API with the client’s credential; your app never calls Meld’s servers directly. Meld requires Portal Android SDK 11.0.0 or later.
For one-time dashboard setup and webhooks, see the Meld integration overview. For the HTTP flow, see the White-Label API guide.

Overview

With portal.ramps.meld you can:
  • Quote a purchase or sale across the providers, and compare their prices and fees
  • Open a provider’s hosted widget, where the user pays and completes any KYC
  • Follow the resulting transactions
  • Describe what each provider supports, where and for how much, with the Network Partner methods
  • Create or find the client’s Meld customer
Every method is a suspend function that returns a Result of the Portal API response, whose data holds the result (null from getNetworkPartnerCountryDefaults when Meld has no defaults for the country). A failed request is returned in the Result, not thrown; see Handling errors. The request and response types are in io.portalhq.android.api.data.meld, and MeldApiError and MeldException in io.portalhq.android.ramps.meld. The same methods are also on portal.api.meld. Prefer portal.ramps.meld: when the calling coroutine is cancelled, for example because its screen closed, portal.ramps.meld throws the CancellationException instead of returning it as a failed Result, so a closed screen isn’t reported as a Meld error.

Prerequisites

Before using Meld, ensure you have: The examples in this guide use a meld value, from inside a coroutine:

Buying crypto with the widget

This guide walks through a purchase. A sale (MeldSessionType.SELL) uses the same quote and widget methods, with crypto as the sourceCurrencyCode, but portal.ramps.meld sends nothing and the widget can’t use the Portal wallet, so your app sends the crypto itself. Transfers (MeldSessionType.TRANSFER) follow a different Meld flow, without quotes, and aren’t covered here.
In a sale, walletAddress is the user’s own Portal address on the chain of sourceCurrencyCode. After the widget, read the address to send to from getRetailTransactionBySession(sessionId) as cryptoDetails?.destinationWalletAddress, never sourceWalletAddress or sessionWalletAddress, and don’t send while it is null. Check that cryptoDetails?.chainId and the transaction’s sourceCurrencyCode match what the user sold, then send exactly the transaction’s sourceAmount, and don’t send while it is null. sourceAmount is a Double: pass it as BigDecimal.valueOf(sourceAmount).toPlainString(), not toString(), which can give 5.0E-4 (see Send tokens): a wrong address or amount means lost funds. See the off-ramp section of Meld’s White-Label API guide.
A purchase takes five steps:
  1. Choose the currency and the wallet address on its chain.
  2. Quote the purchase with getRetailQuote(request) and let the user pick a provider.
  3. Create a widget session at that provider with createRetailWidget(request).
  4. Open the session’s widgetUrl in a Custom Tab, where the user pays.
  5. Follow the transaction with getRetailTransactionBySession(sessionId), or with webhooks to your backend.

Choosing the currency and wallet address

Each Meld crypto code names one asset on one chain: USDC is USDC on Ethereum, and USDC_BASE is USDC on Base. Use the same code in the quote and in the session, and pass a wallet address on that chain, or the purchase settles somewhere the user doesn’t expect. getNetworkPartnerCurrencies(params) returns each currency’s chainId. That is Meld’s own number: the EVM chain id for an EVM currency ("1", "8453"), but other chains have one too (Solana’s is "101"). Build an eip155: chain ID only for an EVM currency, one whose chainId is all digits and whose contract is a 0x address of 40 hex digits (native ETH has the zero address). Map other chains yourself from chainCode, for example Solana to solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp. getAddress returns the wallet’s address for any eip155: chain, so offer only currencies on chains your app supports. For an account abstraction enabled client, offer only the networks Portal supports for it: its smart contract address exists only there.
MyAppException in these examples stands for your app’s own exception type. They call getOrThrow() to keep the flow short; if you catch the exceptions it throws with catch (e: Exception), rethrow CancellationException first.

Quoting

getRetailQuote(request) prices the purchase at every provider that can serve it, and returns one MeldQuote per provider.
MeldGetRetailQuoteRequest takes: The result holds quotes, and error or message when no provider can serve the purchase. A currency pair Meld doesn’t support is refused with a 400 (Meld API error: …) rather than an empty quotes. Each quote carries its serviceProvider, transactionType, the source and destination amounts and currencies, exchangeRate, transactionFee, networkFee, partnerFee, totalFee and paymentMethodType. rampIntelligence can add a rampScore, lowKyc and previouslyUsed, when the customer last used the provider, as an ISO 8601 date-time string.

Creating a widget session

createRetailWidget(request) starts a session at one provider and returns its widgetUrl. Pass the serviceProvider and paymentMethodType of the quote the user picked, so the widget opens at the provider and with the method they chose; the provider prices the purchase again in the widget.
MeldCreateRetailWidgetRequest takes: MeldSessionData takes: The session carries its id, token, widgetUrl, customerId, externalCustomerId and externalSessionId (empty or null when you didn’t pass one).
widgetUrl embeds a single-use session token. Open it once, and don’t log, store or reuse it, including in saved instance state. Create a new session for each purchase.

Opening the widget

Open widgetUrl in a Custom Tab, or the browser, rather than in a WebView: the provider’s page runs its payment, KYC and 3-D Secure steps in the browser. Custom Tabs come from the AndroidX Browser library:
CustomTabsIntent.launchUrl doesn’t tell your app when the user closes the tab, so read the transaction when your screen comes back to the foreground, in onResume. Keep the session’s id, not its widgetUrl, in saved instance state: Android can end your app’s process while the user is in the widget, and recreate your screen when they come back. Launch the Custom Tab on the main thread while your screen is in the foreground, for example inside lifecycle.withResumed { openWidget(session) } after creating the session: Android 10 and later can block an activity start from the background, and the single-use URL would be lost.
See Launch the provider UI in Meld’s docs for how each provider’s page can be shown. Portal returns only widgetUrl, not the serviceProviderWidgetUrl that section mentions.

Following the transaction

getRetailTransactionBySession(sessionId) returns the session’s transaction once Meld has created it, when the user starts the purchase or sale; until then it fails with a 404, as it does for a session that doesn’t exist or isn’t this client’s. The onResume above reads it once when the user comes back; if the user closed the widget before starting the purchase, that read gets the 404. The transaction’s fields are described under Transactions. Since not every provider locks the fields you asked it to, check that the transaction’s destinationCurrencyCode, and cryptoDetails?.destinationWalletAddress and chainId, match what you requested.
Prefer Meld webhooks, delivered to your backend, to repeated reads for transaction updates.

Transactions

searchRetailTransactions(params) lists the client’s transactions, ordered by creation date, and getRetailTransaction(transactionId) returns one.
MeldSearchRetailTransactionsParams filters by statuses, types, serviceProviders, sessionIds, externalSessionIds, serviceProviderTransactionId, amountFrom and amountTo, and from and to, as ISO 8601 dates or date-times. The SDK sends each list comma-separated, and leaves out null and empty values; searchRetailTransactions() without arguments lists the client’s transactions. limit caps the result (Meld’s default is 10, and the most it returns is 100), and the SDK doesn’t expose Meld’s paging cursors: remaining says how many more match, so narrow the filters to see them. A transaction carries its id, sessionId, status, transactionType, serviceProvider, createdAt and updatedAt, and, when Meld has them, the source and destination amounts and currencies and paymentMethodType. Its on-chain details are in cryptoDetails (MeldTransactionCryptoDetails), which can be null, and each of its fields is nullable: blockchainTransactionId, chainId, destinationWalletAddress, sourceWalletAddress, sessionWalletAddress, institution and the fees. serviceProviderDetails is an untyped map that differs by provider.

Customers

A Meld customer is optional: the widget collects the user’s identity itself, and Portal identifies the client to Meld with the same externalCustomerId on every quote and session. Create one when you want to give Meld the user’s details up front.
The client has at most one Meld customer: a second createCustomer fails with a 409, whose meldCustomerId is the existing customer’s id when Portal has it on record. Otherwise, searchCustomer() returns it in customers, and finding it also records it in Portal. Pass as customerId only an id from createCustomer or searchCustomer: Portal doesn’t record the customerId of a widget session, so that id is refused with a 404 (Meld customer does not exist.) until searchCustomer() has found it. createCustomer() without arguments creates a customer with no details. MeldCreateCustomerRequest takes an optional name, email, phone, dateOfBirth (as YYYY-MM-DD; any other format is refused with a 400) and type, MeldCustomerType.INDIVIDUAL or MeldCustomerType.BUSINESS. The SDK and Portal don’t set a type when you leave it out.

Network Partners

The Network Partner methods describe what each provider (partner) supports, where and for how much. Use them to build a country, currency or payment-method picker before you quote.
The SDK requires none of the filters of six of these methods, which you can also call without arguments, but Meld’s guide lists some filters as required, category and country among them, so pass category and, where the method takes them, country, type (currencies) and currencyCode (payment methods). getNetworkPartnerRoutes requires the category, countryCode and both currencies. Most methods name the country country, and routes and country defaults countryCode; a filter named partner takes one provider, and partners several. The category is one of the MeldCategory constants, such as MeldCategory.CRYPTO_ONRAMP, CRYPTO_OFFRAMP or CRYPTO_TRANSFER; Portal refuses any other value for routes and country defaults with a 400.

Handling errors

A failed request is returned in the Result. Read it with onFailure, fold or exceptionOrNull(). Besides network failures (java.io.IOException) and decoding failures (com.google.gson.JsonParseException), three kinds of failure reach your app:
  • Portal API errors: PortalException.Api.HttpBadRequest for a 400, PortalException.Api.HttpUnauthorized for a 401 and PortalException.Api.HttpRequestFailed for any other status. MeldApiError.from(error) reads the HTTP status (null when no HTTP status was received), Portal’s id and the message, and, on the 409 from createCustomer, the existing customer’s meldCustomerId. It returns null for a failure that isn’t an HTTP failure. A 401 has no message: the SDK doesn’t read its body.
  • MeldException.EmptyIdentifier, before any request is sent, when a value that goes in the request path is empty: transactionId, sessionId, the category, countryCode, sourceCurrencyCode or destinationCurrencyCode of getNetworkPartnerRoutes, or the countryCode or category of getNetworkPartnerCountryDefaults. Its name is the parameter’s name. A path value of . or .. fails with an IllegalArgumentException.
  • PortalException.Credentials.PortalCredentialError, before the request is sent, when the credential can’t supply a token, for example with Client Auth after the session ended.
The statuses to expect. The text after Meld API error: is Meld’s own wording and may change: show it to the user, but don’t match on it.
  • 400: an invalid request, with the reason in message, including a provider that needs the user’s IP address and didn’t get one (Meld API error: …), or Meld not enabled or not configured for the environment (Meld integration is not enabled…, Meld API key is not configured…), or Meld rejected the environment’s Meld API key (Meld rejected the configured API key…). Check that each environment has the matching Meld sandbox or production key before you ship.
  • 401: the client’s credential was rejected. With Client Auth, this ends the session: the Portal instance’s onSessionInvalidated listeners fire, and later calls fail with a PortalCredentialError whose reason is SESSION_INVALIDATED.
  • 403 or 422: Meld refused the request (Meld API error: …).
  • 404: Meld customer does not exist., Meld transaction does not exist. or Meld’s own Meld API error: …, and from getRetailTransactionBySession, usually a session whose user hasn’t started paying yet.
  • 409: createCustomer for a client that already has a customer, or Meld’s own Meld API error: ….
  • 502: any other Meld failure, rate limits included.

Values Meld may add

Statuses, categories, payment types, transaction types and the other Meld enumerations are plain String fields (or List<String>), with the values known today as constants in objects such as MeldTransactionStatus, MeldTransactionType, MeldCategory, MeldPaymentType, MeldLockField and MeldCustomerType. A value Meld adds later reaches your app as its string instead of failing the response. Compare against the constants, such as MeldTransactionStatus.SETTLED, and give a when over one an else branch. MeldSessionType is a closed enum, because Portal accepts only its three values.

Testing

Meld, the class of portal.ramps.meld, is an open class whose methods are open, as is MeldApi, the class of portal.api.meld. Pass a Meld to the code that uses it rather than the whole Portal, and a test can give it a subclass or a mock instead.