Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

bitcoin-api

5.2.13 • Public • Published

Bitcoin-API

npm version

Bitcoin API NodeJS Driver and Documentation

Contents

About

The GitHub repo and NPM module bitcoin-api lets you add Bitcoin functionality to your websites and apps. Using a token, you can incorporate Bitcoin deposit and withdraw functionality into your tech.

NodeJS Driver

Table of Contents

Example

'use strict';
 
const BitcoinAPI = require( 'bitcoin-api' );
 
 
const bitcoinAPI = new BitcoinAPI({
 
    token: 'token goes here',
});
 
 
(async () => {
 
    try {
 
        const {
            
            balanceData: { amount }
            
        } = await bitcoinAPI.getTokenInfo();
 
        // logs current balance
        console.log( 'Balance:', amount ); 
 
        // withdraws 0.00004 BTC
        await bitcoinAPI.withdraw({
 
            amount: 0.00004,
            address: 'mgXi9VCAmwaEGszk5yhqkigptTVQM33uhx',
        });
    }
    catch( err ) {
 
        console.log( 'err:', err );
    }
})();

Install

npm install bitcoin-api --save

Setup

BitcoinAPI.createToken

'use strict';
 
const BitcoinAPI = require( 'bitcoin-api' );
 
// Uncomment the line below to enable mainnet mode, the default mode is testnet mode.
// process.env.BITCOIN_API_MAINNET_MODE = 'true';
 
 
(async () => {
 
    const createTokenResults = await BitcoinAPI.createToken();
 
    console.log(
        'Create token results:',
        JSON.stringify( createTokenResults, null, 4  )
    );
 
    /*
        Logs:
 
            Create token results: {
                "token": "6bda5fe7949b470fa4e11e..."
            }
    */
})();

Initialize Instance

'use strict';
 
// Uncomment the line below to enable mainnet mode, the default mode is testnet mode.
// process.env.BITCOIN_API_MAINNET_MODE = 'true';
 
 
const BitcoinAPI = require( 'bitcoin-api' );
 
const bitcoinAPI = new BitcoinAPI({
 
    token: 'asjfhnsdlkjfdf...'
});

Usage

bitcoinAPI.getTokenInfo

(async () => {
 
    const tokenInfo = await bitcoinAPI.getTokenInfo();
 
    console.log( 'Token Info:', JSON.stringify( tokenInfo, null, 4  ) );
 
    /*
        Logs:
 
            Token Info: {
                "balanceData": {
                    "amount": 0.002,
                    "status": "normal"
                }
            }
    */ 
})();

bitcoinAPI.createOrGetAddress

(async () => {
 
    const addressData = await bitcoinAPI.createOrGetAddress();
 
    console.log( 'Address Data:', JSON.stringify( addressData, null, 4 ) );
    
    /*
        Logs:
 
            Address Data: {
                
                "address": "3AfV9QQQTgtCH6YEjBpDTyH5sswgGD5MLp"
            }
 
        Note:
 
            A new address will be created and returned after
            the currently returned address has Bitcoin deposited to it
            with 6 confirmations.
    */
})();

bitcoinAPI.getFeeData

(async () => {
 
    const feeData = await bitcoinAPI.getFeeData();
 
    console.log( 'Fee Data:', JSON.stringify( feeData, null, 4 ) );
    
    /*
        Logs:
 
            Fee Data: {
                "fee": 0.0000001
            }
 
        Note:
 
            This is a public method so it does not
            require a token to be activated.
    */
})();

bitcoinAPI.withdraw

(async () => {
 
    await bitcoinAPI.withdraw({
 
        amount: 0.002,
        address: '3AfV9QQQTgtCH6YEjBpDTyH5sswgGD5MLp',
        includeFeeInAmount: false,
    });
    
    /*
        This operation withdraws 0.002 BTC to 3AfV9QQQTgtCH6YEjBpDTyH5sswgGD5MLp.
 
        Note:
        
            includeFeeInAmount is optional
                When set to true, the fee will automatically
                be deducted from the amount.
 
            enviroWithdrawAmount is optional
                The contribution is added to the withdraw fee.
                This is an option that is used to allow
                users to contribute Bitcoin to the environment🌲🌎!
    */
})();

API

Table of Contents

Requests

API URL

Testnet Base URL:

https://testnet.bitcoin-api.io

Mainnet Base URL:

https://bitcoin-api.io

Example Usage:

A GET request to the https://bitcoin-api.io/fee-data endpoint gets the current fee data associated with doing withdraws. Learn more about the /fee-data endpoint here.

Public Endpoints

Public Endpoints can be accessed using HTTPS requests with the following header:

// HTTP header to add to make requests to Public Endpoints
{
    "Content-Type": "application/json"
}

Tokens

Tokens are used to make authorized requests to the Bitcoin-API API. These authorized requests allow you to access and to control your Bitcoin. A token is a key that gives you access to your Bitcoin, Bitcoin addresses, withdraws, and any other resources associated with that token. It is very important that you keep your token secret and secure in order to protect your Bitcoin and any token-associated data.

Tokens are used as follows:

// HTTP header to add to make requests to Token Endpoints
{
    "Content-Type": "application/json",
    "Token": "6bda5fe7949b470fa4e11e..."
}

Resources

/tokens

POST - Create Token

Create a new token. Tokens provide access to your Bitcoin and associated resources by authorizing token-only requests (i.e. by authorizing requests to endpoints that require "Token Endpoint" authorization).

Authorization

Public Endpoint

Request Body Example
{}
Response Body Example
{
    "token": "asjfhnsdlkjhnsdlsdf..."
}

GET - Get Token Info

This endpoint gets info associated with a token.

Authorization

Token Endpoint

Response Body Example
{
    "balanceData": {
        "amount": 0.0005,
        "status": "normal"
    }
}
  • For the balance data (body.balanceData) returned in the response, there is a status value associated with it. The value will either be "normal" or "transformation". If the value is in the "transformation" state, that means a withdraw is currently being processed.

PUT - Update Token Value

This endpoint updates your token's value. Your old token's value becomes invalid when you update that token's value.

Authorization

Token Endpoint

Request Body Example
{}
Response Body Example
{
    "token": "2asjfhnsfsdkjfhnsdlsdf..."
}

/addresses

POST - Create or Get Bitcoin Address

This endpoint creates or gets a Bitcoin address that can be used to deposit Bitcoin. After using this endpoint to create and return an address, a new address will be created and returned from this endpoint once the currently returned address has Bitcoin sent to it with 6 confirmations. Addresses will always remain associated with the token that's used to create and retrieve them, even after a new address is created and retrieved.

Authorization

Token Endpoint

Request Body Example
{}
Response Body Example
{
    "address": "3AfV9QQQTgtCH6YEjBpDTyH5sswgGD5MLp"
}
More Info:

The balance associated with your token will be automatically updated after your Bitcoin deposit transaction has 6 confirmations on the Bitcoin network. You can retrieve your Bitcoin balance using the /tokens GET General-Token Endpoint.

/fee-data

GET - Get Fee Data

This endpoint gets an estimate of the current fee that's added to the withdraw amount.

Authorization

Public Endpoint

Response Body Example
{
    "fee": 0.000002
}

/withdraws

POST - Do Withdraw

This endpoint withdraws Bitcoin associated with your token.

Authorization

Token Endpoint

Request Body Example
{
    "amount": 0.00004, // 0.00004 is the min withdraw amount
    "address": "3AfV9QQQTgtCH6YEjBpDTyH5sswgGD5MLp",
    "includeFeeInAmount": false, // optional, defaults to false
    "enviroWithdrawAmount": 0.00001 // optional, makes the withdraw an EnviroWithdraw!
}
Response Body Example
{}

EnviroWithdraw - an optional Bitcoin amount is used to allow users to contribute Bitcoin to the environment🌲🌎! This contribution is added to the withdraw fee. How much collected Bitcoin for the environment and how it will be used will be documented here. Open to suggestions for the best ways to use the collected Bitcoin to help the environment.

Upcoming Feature

Counter that shows the sum of all EnviroWithdrawAmount contributions ever made

Estimated Completion Date: late 2020, early 2021


More Information

Technical Support Email: support@bitcoin-api.io

Terms of Service

Privacy Policy

Install

npm i bitcoin-api

DownloadsWeekly Downloads

3,757

Version

5.2.13

License

ISC

Unpacked Size

28.1 kB

Total Files

21

Last publish

Collaborators

  • avatar