Use Case Tutorials
-The following are a collection of potential use cases for the CoinPayments API with example steps for integration.
-Prerequisites for tutorials
--
-
- A CoinPayments.net account. -
- A platform capable of making HTTP calls to the CoinPayments.net API. -
- Developer understanding of the introduction documentation section for the CoinPayments.net API. -
- A private and public API key (from this logged in account page). -
- Knowledge of the different coin codes, listed in the CODE column on the supported coins page. These codes (also known as tickers) are used in the API calls anytime a "currency", "to" or "from" field is needed. -
Note: These tutorials assume every HTTP request executing on https://alpha.coinpayments.net/ and he will be skipped in examples. Also, assume so every API response format to be the default format of JSON.
-Tutorial 1: E-Commerce System Checkout
-This tutorial will cover integrating the following features using the CoinPayments.net API:
--
-
- Get a list of available currencies -
- Returns the currency conversion rates for the specified from currencies converted to the specified to currencies -
- Be notified of a completed payment by the IPN system. -
Part A: Get a list of available currencies
-For getting all available currencies we'll send an HTTP request (GET) to /api/v1/currencies.
- The response will contain information about all available currencies.
- Currency information looks like
{
- "id": 1,
- "type": "crypto",
- "symbol": "BTC",
- "name": "Bitcoin",
- "logo": {
- "imageUrl": "https://api.coinpayments.net/static/img/coins/64x64/1.png",
- "vectorUrl": "https://api.coinpayments.net/static/img/coins/vector/1.svg"
- },
- "decimalPlaces": 8,
- "rank": 1,
- "status": "active",
- "capabilities":
- [
- "multiSigAccounts",
- "singleSigAccounts"
- ],
- "urls": {
- "websites":
- [
- "https://bitcoin.org"
- ],
- "explorers":
- [
- "https://blockchain.info"
- ]
- }
- }
-
- Part B: The currency conversion rates
-For check rate between currencies, we'll send the HTTP request (GET) to /api/v1/rates?from=1&to=5057
- query param explanation:
| Parameter | -Description | -
|---|---|
| from | -currency id to use as the source for rate calculations | -
| to | -comma separated list of currency ids for which to retrieve conversion rates for (from the from currencies) | -
The response will be looks like
-{
- "items": [
- {
- "baseCurrencyId": 1,
- "quoteCurrencyId": 5057,
- "rate": "8896.619359154478102279714028"
- }
- ]
- }
-
- Part C: Checkout
-The next example explains how to create a payment using the CoinPayments.net API in order to accept payment in your e-commerce system during the checkout process. You will need to know the following information in order to create the payment:
--
-
- The total price that you wish to charge for the payment. -
- Buyer personal data (name, email, phone, etc) -
For creating new payment we will send HTTP request (POST) to /api/v1/invoices. The request body should look like
-{
- "clientId": "string",
- "currencyId": 0,
- "invoiceId": "string",
- "buyer": {
- "companyName": "string",
- "name": {
- "firstName": "string",
- "lastName": "string"
- },
- "emailAddress": "user@example.com",
- "phoneNumber": "string",
- "address": {
- "address1": "string",
- "address2": "string",
- "address3": "string",
- "provinceOrState": "string",
- "city": "string",
- "suburbOrDistrict": "string",
- "countryCode": "string",
- "postalCode": "string"
- }
- },
- "description": "string",
- "items": [
- {
- "customId": "string",
- "sku": "string",
- "name": "string",
- "description": "string",
- "quantity": 0,
- "originalAmount": {
- "currencyId": 0,
- "displayValue": "string",
- "value": "string"
- },
- "amount": {
- "currencyId": 0,
- "displayValue": "string",
- "value": "string"
- },
- "tax": {
- "currencyId": 0,
- "displayValue": "string",
- "value": "string"
- }
- }
- ],
- "amount": {
- "breakdown": {
- "subtotal": {
- "currencyId": 0,
- "displayValue": "string",
- "value": "string"
- },
- "shipping": {
- "currencyId": 0,
- "displayValue": "string",
- "value": "string"
- },
- "handling": {
- "currencyId": 0,
- "displayValue": "string",
- "value": "string"
- },
- "taxTotal": {
- "currencyId": 0,
- "displayValue": "string",
- "value": "string"
- },
- "discount": {
- "currencyId": 0,
- "displayValue": "string",
- "value": "string"
- }
- },
- "currencyId": 0,
- "displayValue": "string",
- "value": "string"
- },
- "shipping": {
- "method": "string",
- "companyName": "string",
- "name": {
- "firstName": "string",
- "lastName": "string"
- },
- "emailAddress": "user@example.com",
- "phoneNumber": "string",
- "address": {
- "address1": "string",
- "address2": "string",
- "address3": "string",
- "provinceOrState": "string",
- "city": "string",
- "suburbOrDistrict": "string",
- "countryCode": "string",
- "postalCode": "string"
- }
- },
- "requireBuyerNameAndEmail": true,
- "buyerDataCollectionMessage": "string",
- "notesToRecipient": "string",
- "termsAndConditions": "string",
- "customData": {
- "additionalProp1": "string",
- "additionalProp2": "string",
- "additionalProp3": "string"
- },
- "metadata": {
- "integration": "string",
- "hostname": "string"
- }
- }
-
- -
Request body explanation:
-| Parameter | -Description | -Required | -
|---|---|---|
| clientId | -The id of the client creating this invoice | -Yes | -
| currencyId | -The id of the currency the invoice is to be in, alternatively this can be set individually per field | -No | -
| invoiceId | -The optional API caller provided external invoice number. Appears in screens shown to the Buyer and emails sent. | -No | -
| buyer | -Info about buyer | -No | -
| description | -The purchase description, can be provided instead of a list of items | -No | -
| items | -The optional array of items that a buyer intends to purchase from the merchant | -No | -
| amount | -The total amount of the invoice, with an optional breakdown that provides details, such as the total item amount, total tax amount, shipping, handling, insurance and discounts, if any | -Yes | -
| shipping | -The optional shipping method and address | -No | -
| requireBuyerNameAndEmail | -Flag indicating whether a buyer name and email are required, they will be requested at checkout if not provider by the caller | -No | -
| buyerDataCollectionMessage | -The message to display when collecting buyer user data | -No | -
| notesToRecipient | -Any additional information to share with the buyer about the transaction | -No | -
| termsAndConditions | -Any terms and conditions, e.g. a cancellation policy | -No | -
| customData | -Any custom data the caller wishes to attach to the invoice which will be sent back in notifications | -No | -
| metadata | -- | No | -
Tutorial 2: User Currency Withdrawal
-This tutorial will cover integrating the following features using the CoinPayments.net API.
--
-
- Having a user withdraw an amount of currency from your CoinPayments.net account to a specified currency address (outside the CoinPayments.net system). -
- The withdrawing system checking it's currency balance before initiating the withdrawal. -
Some example scenarios that this tutorial would apply to include:
--
-
- A gambling platform where the user wishes to cash out some of their account's holdings. -
- A freelancer network where a job has been completed and the service provider needs to be paid by the network's system (acting as escrow). -
- A company paying it's employees payroll from their CoinPayments.net wallet. -
To create a transaction and spend funds from an account we will send HTTP request (POST) to
- /api/v1/accounts/{id}/spend. The request body should look like
-
-
- id - The id of the account from which to spend funds from -
{
- "recipients": [
- {
- "address": "string",
- "amount": "string"
- }
- ],
- "memo": "string",
- "customData": {
- "additionalProp1": {},
- "additionalProp2": {},
- "additionalProp3": {}
- }
- }
-
- -
Request body explanation:
-| Parameter | -Description | -Required | -
|---|---|---|
| recipients | -The list of recipients to send funds to | -Yes | -
| memo | -Custom memo to attach to this transaction, this will only be visible within CoinPayments® | -No | -
| customData | -Optional additional information for the spend request e.g. "UseHopAddress" for Ethereum | -No | -
When a request sent successfully then the server will return a response which will contain the next information
-{
- "spendRequestId": "string",
- "spendRequestToken": "string"
- }
-
- The response explanation:
-| Parameter | -Description | -
|---|---|
| spendRequestId | -The id of the created request to spend funds | -
| spendRequestToken | -Additional validation token that must be sent up with the signed request | -
Tutorial 3: Convert Coins
-This tutorial covers converting coins in your CoinPayments.net wallet from one currency to another using the API request /api/v1/accounts/{id}/convert. It also explains how to first check the conversion limits for a coin pairing and confirm that conversion for the given pair is supported. Even though a call to the request will throw an error if the coin pairing is not supported, it's good practice to check the amount you're planning to convert is within the minimum and maximum limits, with the additional benefit of finding out before making the convert request call if the pairing is supported or not.
-For create a transaction and convert funds from an account we'll send HTTP request(POST) to /api/v1/accounts/{id}/convert
--
-
- id - The id of the account for converting -
The request body should look like
-{
- "convertToCurrency": 0,
- "recipients": [
- {
- "address": "string",
- "amount": "string"
- }
- ],
- "memo": "string",
- "customData": {
- "additionalProp1": {},
- "additionalProp2": {},
- "additionalProp3": {}
- }
- }
-
- Request body explanation:
-| Parameter | -Description | -Required | -
|---|---|---|
| convertToCurrency | -Currency into which funds should be converted | -Yes | -
| recipients | -- | Yes | -
| memo | -Custom memo to attach to this transaction, this will only be visible within CoinPayments® | -No | -
| customData | -Optional additional information | -No | -
When a request sent successfully then the server will return a response which will contain the next information
-{
- "spendRequestId": "string",
- "spendRequestToken": "string"
- }
-
- The response explanation:
-| Parameter | -Description | -
|---|---|
| spendRequestId | -The id of the created request to spend funds | -
| spendRequestToken | -Additional validation token that must be sent up with the signed request | -
Tutorial 4: Using the MerchantCallback api endpoints
-This tutorial covers creating callback addresses CoinPayments.net using the API request /api/v1/merchant/callbacks and receiving IPNDTO on your url. It also explains how to list all callback addresses, find the callback address by its id, update information about the callback address and list information about all merchant transactions.
- For sending any of these requests you have to use a pre-request for the authentication. Here is an example in JavaScript: -
-
- var clientId = "7aa5e7ba45d84d978c5ea7f62498abf4";
- var clientKey = "I1sCXrA4jS29f4JYk3mohCoErLHvpESW3XF83sxo/lg=";
- pm.request.headers.add({
- key: "X-CoinPayments-Client",
- value: clientId
- });
- var date = new Date().toUTCString();
- pm.request.headers.add({
- key: "X-CoinPayments-Timestamp",
- value: date
- });
- var text = pm.request.method + pm.request.url + clientId + date + pm.request.body;
- var hash = CryptoJS.HmacSHA256("\ufeff" + text, clientKey);
- var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
- pm.request.headers.add({
- key: "X-CoinPayments-Signature",
- value: hashInBase64
- });
-
-
-
- Receiving IPNDTO
- -
- When merchant, for example, makes a transaction, the request is sent to the url specified for callback address.
-
To receive IPNDTO you should make 3 steps:
-
-
-
- Create callback address by using the request describing below, specify your callback url webhook. -
- Deposit some crypto at the callback address. -
- Receive a ipndto at your callback url webhook. -
Part A: Creating callback addresses
-For creating callback addresses we'll send HTTP request(POST) to /api/v1/merchant/callbacks
-The request body should look like
-{
- "clientId":"7aa5e7ba45d84d978c5ea7f62498abf4",
- "currencyId":4,
- "label":"testcallbacketh",
- "webhook":{
- "nativeCurrencyId":1,
- "url":"https://google.com"
- }
-}
-
- Request body explanation:
-| Parameter | -Description | -Required | -
|---|---|---|
| clientId | -The id of the currency the address will be receiving | -No | -
| currencyId | -The id of the currency the address will be receiving | -Yes | -
| label | -The label of the address (display only) | -No | -
| webhook | -The webhook notification information and customizations | -No | -
When a request sent successfully then the server will return a response which will contain the next information
-{
- "id":"6Fa43sdVgjHuZRMuzei8ae",
- "clientId":"AaXX9g2Zp99ij2cvLVymTN",
- "created":"2020-10-28T09:44:54.9986654+00:00",
- "currencyId":4,
- "address":"0x4ca1a7a8332d4cad0abe4dbcb58c10d6edf4e315",
- "label":"testcallbacketh",
- "webhook":{
- "url":"https://google.com",
- "nativeCurrencyId":1
- }
-}
-
- The response explanation:
-| Parameter | -Description | -
|---|---|
| id | -The unique id of the callback address | -
| clientId | -The merchant client this callback address is linked to | -
| created | -The timestamp of when the callback address was created | -
| currencyId | -The id of the currency the address is receiving | -
| address | -The actual deposit address | -
| label | -The display label of the callback address | -
| webhook | -The webhook notification information and customizations | -
Part B: Sending a request to spend funds from the account
-This part was described in the Tutorial 2.
- - After making these steps the request will be sent on your url. -The body of the request contains next information:
-{
- "id": "bdaae1f4c051445099325f384a74e46b",
- "type": "CallbackDepositConfirmed",
- "timestamp": "2020-10-15T13:16:56.27704444+00:00",
- "transaction": {
- "callbackAddressId": "Lhdrs8hw6z3WWpHD6oMBea",
- "address": "0x4723e2edcdedd471e016b03765df8f9c56572c69",
- "currency": {
- "id": "4",
- "symbol": "ETH",
- "name": "Ethereum",
- },
- "amount": {
- "currencyId": "0",
- "displayValue": "0.000000000000000001",
- "value": "1"
- },
- "coinPaymentsFee": {
- "currencyId": "0",
- "displayValue": "0.000000000000000000",
- "value": "0"
- },
- "nativeCurrency": {
- "id": "1",
- "symbol": "BTC",
- "name": "Bitcoin",
- },
- "nativeAmount": {
- "currencyId": "0",
- "displayValue": "0.00000000",
- "value": "0"
- },
- "nativeCoinPaymentsFee": {
- "currencyId": "0",
- "displayValue": "0.00000000",
- "value": "0"
- },
- "status": "Confirmed"
- }
-}
-
- The response explanation:
-| Parameter | -Description | -
|---|---|
| id | -The unique id of the ipn notification | -
| type | -The type of notification | -
| timestamp | -The timestamp of when the notification was generated | -
| invoice | -The invoice the notification was sent for | -
| payouts | -The payout information of the invoice, once available | -
| transaction | -Callback deposit transaction | -
| customdata | -Any custom data associated with the callback address, specified at the time when the callback address was created | -
Other requests which can be helpful for working with callbacks:
- -List of all callback addresses
-For list all callback addresses sorted descending by the creation date we'll send HTTP request(GET) to /api/v1/merchant/callbacks
--
-
- clientId - The merchant client id whose callback address should be listed -
- currencyId - The id of the currency the address was receiving -
- after - -
- limit - -
When a request sent successfully then the server will return a response which will contain the next information
-{
- "items":[{
- "id":"6Fa43sdVgjHuZRMuzei8ae",
- "clientId":"AaXX9g2Zp99ij2cvLVymTN",
- "created":"2020-10-28T09:44:54.998665+00:00",
- "currencyId":4,
- "address":"0x4ca1a7a8332d4cad0abe4dbcb58c10d6edf4e315",
- "label":"testcallbacketh",
- "webhook":{
- "url":"https://google.com",
- "nativeCurrencyId":1
- }
- }],
- "paging":{
- "cursors":{
- "before":"WpESICZ72Ag=",
- "after":"At0ZPLdf2Ag="
- },
- "limit":100
- }
-}
-
- The response explanation:
-| Parameter | -Description | -
|---|---|
| items | -Information about the callback address | -
| paging | -- |
Searching the callback address by id
-For listing all callback addresses with the same id we'll send HTTP request(GET) to /api/v1/merchant/callbacks/{id}
--
-
- id - The id of the callback address -
When a request sent successfully then the server will return a response which will contain the next information
-{
- "id":"56NVoGgbkPxStkhTjokV8E",
- "clientId":"AaXX9g2Zp99ij2cvLVymTN",
- "created":"2020-09-28T13:43:10.01129+00:00",
- "currencyId":4,
- "address":"0xbb050a0ab1e6a801ed6d2c7eac775737dea7d11e",
- "label":"testcallbacketh",
- "webhook":{
- "url":"https://google.com",
- "nativeCurrencyId":1
- }
-}
-
- The response explanation:
-| Parameter | -Description | -
|---|---|
| id | -The unique id of the callback address | -
| clientId | -The merchant client this callback address is linked to | -
| created | -The timestamp of when the callback address was created | -
| currencyId | -The id of the currency the address is receiving | -
| address | -The actual deposit address | -
| label | -The display label of the callback address | -
| webhook | -The webhook notification information and customizations | -
Updating a callback address
-For updating a callback address we'll send HTTP request(PUT) to /api/v1/merchant/callbacks/{id}
--
-
- id - The id of the callback address -
The request body should look like
-{
- "clientId":"7aa5e7ba45d84d978c5ea7f62498abf4",
- "currencyId":4,
- "label":"testcallbacketh",
- "webhook":{
- "nativeCurrencyId":1,
- "url":"https://google.com"
- }
-}
-
- Request body explanation:
-| Parameter | -Description | -Required | -
|---|---|---|
| label | -The label of the address (display only) | -No | -
| webhook | -The webhook notification information and customizations | -No | -
When a request sent successfully then the server will return a response which will contain the status 204(No content)
- - - - -Listing all deposit transactions to callback addresses
-For listing all deposit transactions to callback addresses, ordered newest first and optionally filtering by address, currency and date range we'll send HTTP request(GET) to /api/v1/merchant/callbacks/{id}
--
-
- callbackId - The id of the callback address -
- currencyId - -
- from - -
- to - -
- after - -
- limit - - -
When a request sent successfully then the server will return a response which will contain the next information
-{
- "items":[{
- "id":"Dv1vDiDmfVrgSkEB2bLcUA",
- "created":"2020-09-25T08:36:23.470791+00:00",
- "completed":"2020-09-25T08:36:23.470793+00:00",
- "callbackAddressId":"JhmojzDdEJA8qJ4fF3zkT9",
- "address":"V7dHXKN6jKFXQrV3AKsYiePNezcgf7Cn2h",
- "currency":{
- "id":"33","symbol":"VLX",
- "name":"Velas","decimalPlaces":18},
- "nativeCurrency":{
- "id":"1",
- "symbol":"BTC",
- "name":"Bitcoin",
- "decimalPlaces":8
- },
- "amount":{
- "displayValue":"81.282438450358048310",
- "value":"81282438450358048310",
- "amount":"81282438450358048310",
- "currencyId":"0"
- },
- "coinPaymentsFee":{
- "displayValue":"0.406412192251790242",
- "value":"406412192251790242",
- "amount":"406412192251790242",
- "currencyId":"0"
- },
- "nativeAmount":{
- "displayValue":"0.00030505",
- "value":"30505",
- "amount":"30505",
- "currencyId":"1"
- },
- "nativeCoinPaymentsFee":{
- "displayValue":"0.00000153",
- "value":"153",
- "amount":"153",
- "currencyId":"1"
- },
- "status":"PaidOut"
- }],
- "paging":{
- "cursors":{
- "before":"xnPHFS5h2Ag=",
- "after":"TPRdkbdf2Ag="
- },
- "limit":100
- }
-}
-
- The response explanation:
-| Parameter | -Description | -
|---|---|
| items | -Information about callback address | -
| paging | -- |