# @coingecko/cg-api-ts

> JavaScript wrapper for coingecko.com/api

Latest version **0.0.51** (published 2021-04-25) · ISC license · 0 weekly downloads

## Install

```sh
npm install @coingecko/cg-api-ts
pnpm add @coingecko/cg-api-ts
yarn add @coingecko/cg-api-ts
bun add @coingecko/cg-api-ts
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.51 |
| Published | 2021-04-25 |
| First published | 2019-08-13 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 141.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | tmlee, jon_coingecko, superoo7 |

## Links

- npm: https://www.npmjs.com/package/@coingecko/cg-api-ts
- npm.io page: https://npm.io/package/@coingecko/cg-api-ts

## Recent versions

- 0.0.51 (latest) — 2021-04-25
- 0.0.50 — 2021-04-01
- 0.0.49 — 2021-04-01
- 0.0.48 — 2021-04-01
- 0.0.47 — 2021-03-18
- 0.0.46 — 2021-03-12
- 0.0.45 — 2021-02-15
- 0.0.44 — 2021-01-13
- 0.0.43 — 2021-01-12
- 0.0.41 — 2020-11-09
- 0.0.40 — 2020-10-19
- 0.0.39 — 2020-10-19
- 0.0.38 — 2020-10-16
- 0.0.37 — 2020-10-14
- 0.0.36 — 2020-09-22
- … 34 more at https://npm.io/package/@coingecko/cg-api-ts/versions

## README

# CoinGecko API Wrapper

## Installation

```
npm i --save @coingecko/cg-api-ts
yarn add @coingecko/cg-api-ts
```

## Available Endpoint

|            Endpoint             | Status | Testing |            Function            |
| :-----------------------------: | :----: | :-----: | :----------------------------: |
|              /ping              |  [/]   |   [/]   |            getPing             |
|          /simple/price          |  [/]   |   [/]   |         getSimplePrice         |
| /simple/supported_vs_currencies |  [/]   |   [/]   | getSimpleSupportedVsCurrencies |
|    /simple/token_price/{id}     |  [x]   |   [x]   |                                |
|           /coins/list           |  [/]   |   [/]   |          getCoinsList          |
|         /coins/markets          |  [x]   |   [x]   |                                |
|           /coins/{id}           |  [/]   |   [x]   |           getCoinsId           |
|    /coins/{id}/market_chart     |  [/]   |   [/]   |     getCoinsIdMarketChart      |
|   /coins/{id}/status_updates    |  [/]   |   [/]   |    getCoinsIdStatusUpdates     |
|         /status_updates         |  [/]   |   [/]   |        getStatusUpdates        |
|             /global             |  [/]   |   [/]   |           getGlobal            |

## Usage

```ts
import { CoinGeckoAPI } from "@coingecko/cg-api-ts";

const cg = new CoinGeckoAPI(fetch);
// sometimes fetch is not bind properly need to use following:
// export const cg = new CoinGeckoAPI(window.fetch.bind(window));

cg.getPing()
  .then(({ data, response, endpoint }) => {
    console.log(data);
    console.log(response);
    console.log(endpoint);
  })
  // Error Handling
  .catch(err => {
    if (err.type === APIError.SERVER) {
      // Resouces not found (Error 404)
      console.error("API resources not found");
    } else if (err.type === APIError.DOWN) {
      // CoinGecko API Down (Error 500)
      console.error("API Down");
    } else if (err.type === APIError.TIMEOUT) {
      // API Request timeout after 20s (default)
      console.error("API Timeout");
      console.error(err.message); // Request timeout after 20000ms
    } else {
      console.error(err.message);
    }
  });
```

### Browser

CoinGeckoAPI class used fetch API, which requires browser support for Fetch API. Consider using [Polyfill library](https://github.com/github/fetch)

#### Promise style

```ts
import { CoinGeckoAPI, APIError } from "@coingecko/cg-api-ts";

const cg = new CoinGeckoAPI(fetch);
cg.getPing().then(({ data, response }) => {
  if (data) {
    console.log("Server online");
  } else {
    console.error("Server not responding properly");
  }
});
```

#### Async Await

```ts
(async () => {
  const cg = new CoinGeckoAPI(fetch);
  const { data, response } = await cg.getPing();
  if (data) {
    console.log("Server online");
  } else {
    console.error("Server offline");
  }
})();
```

### Nodejs

```ts
const nodeFetch = require("node-fetch");
const cg = new CoinGeckoAPI(nodeFetch);
cg.getPing().then(({ data, response }) => {
  if (data) {
    console.log("Server online");
  } else {
    console.error("Server offline");
  }
});
```

---
_Source: https://npm.io/package/@coingecko/cg-api-ts · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
