Skip to content

ImSeaWorld/namebasejs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NameBaseJS 1.2.0

Promise based namebase.io API wrapper

Usage

npm install namebasejs --save

This wrapper covers up to 90% of the visible endpoints on namebase currently. Submit an issue or PR to fill in the ones I'm missing.

You can get your session from the network tab of Inspect Element under namebase-main. Copy everything after the =. Although, you don't need to instantiate namebasejs with a session, you can also use the local login which will store the session from your login in _auth_session which can be gotten from the api namebasejs.session at any time.

NOTE: Currently NameBase is returning auth0 headers to prevent you from using their API too frequently.

1.2.0 - Added handling for auth0 headers, this can be disabled if it's not working correctly or you want to handle everything yourself. You can read more about auth0 headers here. Disable auth0 like so(after instantiation of namebasejs): namebasejs.auth0 = false; This doesn't stop you from using the API, it just doesn't handle the auth0 headers for you. It will still keep track of the auth0 headers and are available at namebasejs.auth0_headers.

Example: Login using email & password

import NameBaseJS from "namebasejs";

const namebasejs = new NameBaseJS();

namebasejs.auth.login('email', 'password')
    .then(({ data, status, headers, session }) => {
        // This is the only function that returns 'session'
        console.log('My current session: ', session);

        if (status === 200) {
            console.log('Logged in successfully!');
            
            namebasejs.user.self().then(({ data }) => {
                console.log('Current HNS Balance: ', data.hns_balance / 1000000); // convert little to big
            });
        }
    })
    .catch(console.error);

Example: Login using session

import NameBaseJS from "namebasejs";

const namebasejs = new NameBaseJS({session: 'SESSION_TOKEN'});

namebasejs.user.self().then(({ data }) => {
    console.log('Current HNS Balance: ', data.hns_balance / 1000000); // convert little to big
});

Example: Login using Access and Secret keys

import NameBaseJS from "namebasejs";

const namebasejs = new NameBaseJS({
    aKey: '1dXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', // 64 Character Access Key
    sKey: 'a2XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' // 64 Character Secret Key
});

// API Keys will restrict the endpoints available to you.
// It only allows the endpoints of:
// - Account
// - Marketplace
// - Trade
// - DNS
// - Ticker
// - a few Domain endpoints

namebasejs.account.self().then(({ data }) => {
    const assetHNS = data.balances.find(b => b.asset === 'HNS');

    console.log('Current HNS Balance: ', assetHNS.unlocked);
    console.log('Order Locked HNS Balance: ', assetHNS.lockedInOrders);
});

Testing with Jest [Broken Currently]

  • Clone repository: https://github.com/ImSeaWorld/namebasejs.git
  • npm install
  • npm run test

Result

We're now using Axios! This means we can now be used in the browser and NodeJS! Referring to the Axios documentation, the response schema is as follows:

{
    data: {}, // The response that was provided by the API
    status: 200, // HTTP status code from the server response
    statusText: 'OK', // HTTP status message from the server response
    headers: {}, // The headers that the server responded with
    config: {}, // The config that was provided to `axios` for the request
    request: {} // The request that generated this response
}

All methods return AxiosPromise, the example above is how EVERY method is returned.

Any parameter with a ? should be considered optional.

NameBaseJS

  • namebasejs({ session?: string, aKey?: string, sKey?: string }) - Constructor
  • namebasejs.axios - The instance of axios used for requests.
  • namebasejs.auth0 - Boolean to enable/disable auth0 headers. Default: true
  • namebasejs.auth0_headers - The auth0 object populated by the headers that are returned from the server. This does include now() function to get the current time(unix).
  • namebasejs.receive_window - The receive window for timed requests. Default: 10000(ms)
  • namebasejs.enums - The enums used for the API.
  • namebasejs.session - The session token used for requests. Default: null
  • namebasejs.auth_key - The access key used for requests. Set with colon delimited access and secret key! Default: null
  • namebasejs.request(_interface, method, paylod?, ...args?) - The request method used for all requests. This is the method that handles the auth0 headers. This method is not meant to be used directly, but is exposed for advanced usage.
  • namebasejs.timedRequest(_interface, method, payload?, ...args?) - The timed request method used for all requests that require a receive_window. This method is not meant to be used directly, but is exposed for advanced usage.
  • namebasejs.account - The account interface.
  • namebasejs.auction - The auction interface.
  • namebasejs.auth - The auth interface.
  • namebasejs.dns - The dns interface.
  • namebasejs.domain - 1.2.0 The domain interface.
  • namebasejs.domains - The domains interface.
  • namebasejs.fiat - The fiat interface.
  • namebasejs.marketplace - The marketplace interface.
  • namebasejs.ticker - The ticker interface.
  • namebasejs.trade - The trade interface.
  • namebasejs.user - The user interface.

Account

Auction

  • auction.bid(domain, bidAmount, blindAmount) - Bid on a domain

Auth

  • auth.login(email, password, token?) - Login using email, password and 2fa token if enabled
  • auth.logout() - Logout of current session
  • auth.apiKeys() - Get all API Keys
  • auth.apiKeyCreate(name) - Create an API Key
  • auth.apiKeyDelete(accessKey) - Delete an API Key

DNS

Domain - 1.2.0

  • domain.get(domain) - 1.2.0 Get domain info
  • domain.watch(domain) - 1.2.0 Watch a domain for updates(toggles)
  • domain.giftSLD(domain, recipientEmail, senderName, note) - 1.2.0 Gift a domain to a friend
  • domain.giftTLD(domain, recipientEmail, senderName, note) - 1.2.0 Gift a domain to a friend

Domains

Fiat

  • fiat.accounts()
  • fiat.transfers()

Marketplace

Ticker

Trade

User

  • user.self() - Get current user
  • user.wallet() - 1.1.2
  • user.domainSummary() - 1.1.2 Domain summary dashboard widget
  • user.messages() - 1.1.2
  • user.referralStats(limit?) - 1.1.2 Referral stats dashboard widget
  • user.pendingHistory() - Pending history dashboard widget
  • user.domains(offset?, sortKey?, sortDirection?, limit?) - Unlisted domains
  • user.transferredDomains(offset?, sortKey?, sortDirection?, limit?) - Domain transfer history
  • user.listedDomains(offset?, limit?) - Domains listed for sale
  • user.mfa() - Check if multifactor authentication is enabled
  • user.offersSent(offset?, sortKey?, sortDirection?)
  • user.offersReceived(offset?, sortKey?, sortDirection?)
  • user.offersNotifications() - Offer notification widget
  • user.openBids(offset?) - Open bids on active auctions
  • user.lostBids(offset?) - Lost bids on ended auctions
  • user.revealingBids(offset?) - Bids that are currently in reveal