2015-10-19 13:22:16 +08:00
# Migration 0.13.0 -> 0.14.0
web3.js version 0.14.0 supports [multiple instances of web3 ](https://github.com/ethereum/web3.js/issues/297 ) object.
To migrate to this version, please follow the guide:
```diff
-var web3 = require('web3');
+var Web3 = require('web3');
+var web3 = new Web3();
```
2014-09-30 22:55:07 +02:00
# Ethereum JavaScript API
2015-04-20 12:08:42 +02:00
[](https://gitter.im/ethereum/web3.js?utm_source=badge& utm_medium=badge& utm_campaign=pr-badge& utm_content=badge)
2015-03-22 13:08:22 +00:00
2015-01-27 00:54:21 +01:00
This is the Ethereum compatible [JavaScript API ](https://github.com/ethereum/wiki/wiki/JavaScript-API )
2015-02-27 12:41:07 +01:00
which implements the [Generic JSON RPC ](https://github.com/ethereum/wiki/wiki/JSON-RPC ) spec. It's available on npm as a node module, for bower and component as an embeddable js and as a meteor.js package.
2014-09-30 22:55:07 +02:00
2016-06-29 04:32:43 -05:00
[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![dependency status][dep-image]][dep-url] [![dev dependency status][dep-dev-image]][dep-dev-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Stories in Ready][waffle-image]][waffle-url]
2014-11-05 22:33:28 +02:00
2014-11-10 15:26:10 +02:00
<!-- [](https://ci.testling.com/ethereum/ethereum.js) -->
2014-11-05 22:33:28 +02:00
2016-01-05 15:06:29 +01:00
You need to run a local Ethereum node to use this library.
2015-02-27 12:41:07 +01:00
2015-02-27 09:54:39 +01:00
[Documentation ](https://github.com/ethereum/wiki/wiki/JavaScript-API )
2014-11-06 22:51:37 +02:00
## Installation
2014-11-05 19:46:01 +02:00
2014-11-06 22:51:37 +02:00
### Node.js
2014-11-05 19:46:01 +02:00
2015-07-13 15:58:38 +02:00
```bash
npm install web3
```
2015-02-27 09:54:39 +01:00
2017-08-10 05:08:55 -07:00
### Yarn
```bash
yarn add web3
```
2015-02-27 09:54:39 +01:00
### Meteor.js
2015-07-13 15:58:38 +02:00
```bash
meteor add ethereum:web3
```
2014-11-05 19:46:01 +02:00
2015-03-09 10:28:14 +01:00
### As Browser module
2014-11-05 19:46:01 +02:00
Bower
2015-07-13 15:58:38 +02:00
```bash
bower install web3
```
2014-11-05 19:46:01 +02:00
Component
2015-07-13 15:58:38 +02:00
```bash
component install ethereum/web3.js
```
2014-11-05 19:46:01 +02:00
2016-06-27 20:37:18 +08:00
* Include `web3.min.js` in your html file. (not required for the meteor package)
2014-11-05 19:46:01 +02:00
2014-11-06 22:51:37 +02:00
## Usage
2015-04-13 12:15:45 +02:00
Use the `web3` object directly from global namespace:
2015-04-10 00:27:47 +02:00
2015-07-13 15:58:38 +02:00
```js
console.log(web3); // {eth: .., shh: ...} // it's here!
```
2015-04-10 00:27:47 +02:00
2015-07-13 15:55:19 +02:00
Set a provider (HttpProvider)
2014-11-05 19:46:01 +02:00
2015-07-13 15:58:38 +02:00
```js
2017-07-27 09:40:24 -04:00
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
// set the provider you want from Web3.providers
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
```
Set a provider (HttpProvider using [HTTP Basic Authentication ](https://en.wikipedia.org/wiki/Basic_access_authentication ))
```js
web3.setProvider(new web3.providers.HttpProvider('http://host.url', 0, BasicAuthUsername, BasicAuthPassword));
2015-07-13 15:58:38 +02:00
```
2014-11-05 19:46:01 +02:00
There you go, now you can use it:
2015-07-13 15:58:38 +02:00
```js
2015-01-27 00:54:21 +01:00
var coinbase = web3.eth.coinbase;
2015-02-19 18:37:09 +01:00
var balance = web3.eth.getBalance(coinbase);
2014-11-05 19:46:01 +02:00
```
2015-07-13 15:58:38 +02:00
You can find more examples in [`example` ](https://github.com/ethereum/web3.js/tree/master/example ) directory.
2014-11-05 19:46:01 +02:00
2015-02-27 09:54:39 +01:00
2015-01-11 17:54:36 +01:00
## Contribute!
### Requirements
* Node.js
* npm
```bash
sudo apt-get update
sudo apt-get install nodejs
sudo apt-get install npm
sudo apt-get install nodejs-legacy
```
2015-01-15 14:30:34 +01:00
### Building (gulp)
2014-11-05 19:46:01 +02:00
2015-01-15 14:30:34 +01:00
```bash
2015-01-11 17:54:36 +01:00
npm run-script build
```
2014-11-05 19:46:01 +02:00
2015-01-15 14:30:34 +01:00
### Testing (mocha)
2014-09-30 22:55:07 +02:00
2015-01-15 14:30:34 +01:00
```bash
2015-01-11 17:54:36 +01:00
npm test
```
2017-07-27 09:40:24 -04:00
### Community
- [Gitter ](https://gitter.im/ethereum/web3.js?source=orgpage )
- [Forum ](https://forum.ethereum.org/categories/ethereum-js )
### Other implementations
- Python [Web3.py ](https://github.com/pipermerriam/web3.py )
- Haskell [hs-web3 ](https://github.com/airalab/hs-web3 )
- Java [web3j ](https://github.com/web3j/web3j )
2015-04-10 10:07:15 +02:00
[npm-image]: https://badge.fury.io/js/web3.png
[npm-url]: https://npmjs.org/package/web3
[travis-image]: https://travis-ci.org/ethereum/web3.js.svg
[travis-url]: https://travis-ci.org/ethereum/web3.js
[dep-image]: https://david-dm.org/ethereum/web3.js.svg
[dep-url]: https://david-dm.org/ethereum/web3.js
2015-06-10 12:14:44 +02:00
[dep-dev-image]: https://david-dm.org/ethereum/web3.js/dev-status.svg
2015-04-10 10:07:15 +02:00
[dep-dev-url]: https://david-dm.org/ethereum/web3.js#info =devDependencies
[coveralls-image]: https://coveralls.io/repos/ethereum/web3.js/badge.svg?branch=master
[coveralls-url]: https://coveralls.io/r/ethereum/web3.js?branch=master
[waffle-image]: https://badge.waffle.io/ethereum/web3.js.svg?label=ready& title=Ready
2017-06-05 20:33:32 +02:00
[waffle-url]: https://waffle.io/ethereum/web3.js