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 PortalSwift 8.2.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
Withportal.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
MeldResponse whose data holds the result (a MeldNullableResponse, whose data can be nil, for getNetworkPartnerCountryDefaults). The same methods are also on portal.api.meld.
Prerequisites
Before using Meld, ensure you have:- A properly initialized Portal client with a wallet (see Getting started and Create a wallet)
- Meld enabled for your environment in the Portal Dashboard (see Meld integration overview)
portal instance:
Buying crypto with the widget
This guide walks through a purchase. A sale (.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 (.transfer) follow a different Meld flow, without quotes, and aren’t covered here.
A purchase takes five steps:
- Choose the currency and the wallet address on its chain.
- Quote the purchase with
getRetailQuote(request:)and let the user pick a provider. - Create a widget session at that provider with
createRetailWidget(request:). - Open the session’s
widgetUrlin the browser, where the user pays. - 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.
MyAppError in these examples stands for your app’s own error type.
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.
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 nil when you didn’t pass one).
Opening the widget
OpenwidgetUrl in the browser, for example in an SFSafariViewController, rather than in a WKWebView: the provider’s page runs its payment and KYC steps in the browser. Present it on the main actor:
widgetUrl, not the serviceProviderWidgetUrl that section mentions.
Following the transaction
When the user closes the widget,safariViewControllerDidFinish(_:) is called: read the session’s transaction then. getRetailTransactionBySession(sessionId:) returns it 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.
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. A list filter matches any of its 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 nil, and each of its fields is optional: blockchainTransactionId, chainId, destinationWalletAddress, sourceWalletAddress, sessionWalletAddress, institution and the fees.
Customers
A Meld customer is optional: the widget collects the user’s identity itself, and Portal identifies the client to Meld with the sameexternalCustomerId on every quote and session. Create one when you want to pre-fill KYC fields.
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, .individual or .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.category and country as required for most of them, so pass them. 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 a MeldCategory, such as .cryptoOnramp, .cryptoOfframp or .cryptoTransfer.
Handling errors
Besides transport and decoding errors, three kinds of error reach your app:- Portal API errors, thrown as
PortalRequestsError.MeldApiError.from(_:)reads the HTTPstatus, Portal’sidand themessage, and, on the409fromcreateCustomer, the existing customer’smeldCustomerId. It returnsnilfor an error that isn’t an HTTP failure. MeldError.emptyIdentifier(name:), thrown before any request is sent when an id that goes in the request path, such astransactionIdorsessionId, is empty.PortalCredentialError, with Client Auth, when the session is gone before the request is sent.
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 inmessage, 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…).401: the client’s credential was rejected, or Meld rejected the environment’s Meld API key (PortalRequestsError.unauthorized). With Client Auth, either 401 ends the session:onSessionInvalidatedfires and later calls throwPortalCredentialError.sessionInvalidated. Check that each environment has the matching Meld sandbox or production key before you ship.403or422: Meld refused the request (Meld API error: …).404:Meld customer does not exist.,Meld transaction does not exist.or Meld’s ownMeld API error: …, and fromgetRetailTransactionBySession, usually a session whose user hasn’t started paying yet.409:createCustomerfor a client that already has a customer, or Meld’s ownMeld 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 open values: structs that conform toMeldOpenValue, with a rawValue and a static constant for each known value, such as MeldTransactionStatus.settled. A value Meld adds later still decodes, with its rawValue, instead of failing the response. Compare against the known constants, and give a switch over one a default case. MeldSessionType is a closed Swift enum.
Testing
portal.ramps.meld is a MeldProtocol, and PortalApiProtocol also exposes the methods as meld. To use your own implementation in a PortalProtocol test double, build the namespace around your NoahProtocol and MeldProtocol conformances with Ramps(noah:meld:); Meld(api:) wraps a fake PortalMeldApiProtocol. If you conform to PortalApiProtocol yourself, add its new meld member.
Related documentation
- Meld integration overview: dashboard setup
- White-Label API guide: the HTTP flow
- Meld webhooks: transaction updates