├── .gitignore
├── Dockerfile
├── README.md
├── package-lock.json
├── package.json
├── sample-v3.js
└── sample-v4.js
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
--------------------------------------------------------------------------------
/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM node:8.16
2 |
3 | ENV APP_HOME /usr/src/app/
4 |
5 | WORKDIR $APP_HOME
6 |
7 | COPY package.json $APP_HOME/package.json
8 |
9 | RUN npm install
10 |
11 | COPY ./ $APP_HOME
12 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # The Odds API Code Samples (v4) - NodeJS
2 |
3 | The Odds API provides live odds for loads of sports from bookmakers around the world, in an easy to use JSON format.
4 |
5 | Before getting started, be sure to get a free API key from [https://the-odds-api.com](https://the-odds-api.com)
6 |
7 | For more info on the API, [see the docs](https://the-odds-api.com/liveapi/guides/v4/)
8 |
9 |
10 |
11 | ## Get Started
12 |
13 | ```
14 | node sample-v4.js YOUR_API_KEY
15 | ```
16 |
17 | This will print:
18 | - A list of in-season sports
19 | - Events and odds for the next 8 upcoming games (across all sports)
20 | - Requests used & remaining for your api key
21 |
22 | To change the sport, region and market, see the parameters specified at the beginning sample-v4.js
23 |
24 | Make sure the axios library is installed `npm install axios`
25 |
26 |
27 |
28 | ---
29 |
30 |
31 |
32 | ## Using Docker (Mac and Linux)
33 |
34 | Build the image
35 |
36 | ```
37 | docker build -t theoddsapi/sample:latest .
38 |
39 | ```
40 |
41 | Run the node script in the container
42 | ```
43 | docker run -t -i --rm -v "$(pwd)":/usr/src/app/ theoddsapi/sample:latest node sample-v4.js YOUR_API_KEY
44 | ```
45 |
46 | ## Older API versions
47 |
48 | These samples will work for older API versions by running the relevant `sample-{version}.py` file.
49 |
--------------------------------------------------------------------------------
/package-lock.json:
--------------------------------------------------------------------------------
1 | {
2 | "requires": true,
3 | "lockfileVersion": 1,
4 | "dependencies": {
5 | "asynckit": {
6 | "version": "0.4.0",
7 | "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
8 | "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q=="
9 | },
10 | "axios": {
11 | "version": "1.7.4",
12 | "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.4.tgz",
13 | "integrity": "sha512-DukmaFRnY6AzAALSH4J2M3k6PkaC+MfaAGdEERRWcC9q3/TWQwLpHR8ZRLKTdQ3aBDL64EdluRDjJqKw+BPZEw==",
14 | "requires": {
15 | "follow-redirects": "^1.15.6",
16 | "form-data": "^4.0.0",
17 | "proxy-from-env": "^1.1.0"
18 | }
19 | },
20 | "combined-stream": {
21 | "version": "1.0.8",
22 | "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
23 | "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
24 | "requires": {
25 | "delayed-stream": "~1.0.0"
26 | }
27 | },
28 | "delayed-stream": {
29 | "version": "1.0.0",
30 | "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
31 | "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ=="
32 | },
33 | "follow-redirects": {
34 | "version": "1.15.6",
35 | "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz",
36 | "integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA=="
37 | },
38 | "form-data": {
39 | "version": "4.0.0",
40 | "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz",
41 | "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==",
42 | "requires": {
43 | "asynckit": "^0.4.0",
44 | "combined-stream": "^1.0.8",
45 | "mime-types": "^2.1.12"
46 | }
47 | },
48 | "mime-db": {
49 | "version": "1.52.0",
50 | "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
51 | "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg=="
52 | },
53 | "mime-types": {
54 | "version": "2.1.35",
55 | "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz",
56 | "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==",
57 | "requires": {
58 | "mime-db": "1.52.0"
59 | }
60 | },
61 | "proxy-from-env": {
62 | "version": "1.1.0",
63 | "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
64 | "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "dependencies": {
3 | "axios": "^1.7.4"
4 | }
5 | }
6 |
--------------------------------------------------------------------------------
/sample-v3.js:
--------------------------------------------------------------------------------
1 |
2 | const axios = require('axios')
3 |
4 | // An api key is emailed to you when you sign up to a plan
5 | // Get a free API key at https://api.the-odds-api.com/
6 | const api_key = process.argv[2] || 'YOUR_API_KEY'
7 |
8 | const sport_key = 'upcoming' // use the sport_key from the /sports endpoint below, or use 'upcoming' to see the next 8 games across all sports
9 |
10 | const region = 'uk' // uk | us | eu | au
11 |
12 | const market = 'h2h' // h2h | spreads | totals
13 |
14 |
15 | /*
16 | First get a list of in-season sports
17 | the sport 'key' from the response can be used to get odds in the next request
18 |
19 | */
20 | axios.get('https://api.the-odds-api.com/v3/sports', {
21 | params: {
22 | api_key: api_key
23 | }
24 | })
25 | .then(response => {
26 | console.log(response.data.data)
27 | })
28 | .catch(error => {
29 | console.log('Error status', error.response.status)
30 | console.log(error.response.data)
31 | })
32 |
33 |
34 | /*
35 | Now get a list of live & upcoming games for the sport you want, along with odds for different bookmakers
36 |
37 | */
38 | axios.get('https://api.the-odds-api.com/v3/odds', {
39 | params: {
40 | api_key: api_key,
41 | sport: sport_key,
42 | region: region,
43 | mkt: market,
44 | }
45 | })
46 | .then(response => {
47 | // response.data.data contains a list of live and
48 | // upcoming events and odds for different bookmakers.
49 | // Events are ordered by start time (live events are first)
50 | console.log(JSON.stringify(response.data.data))
51 |
52 | // Check your usage
53 | console.log('Remaining requests',response.headers['x-requests-remaining'])
54 | console.log('Used requests',response.headers['x-requests-used'])
55 |
56 | })
57 | .catch(error => {
58 | console.log('Error status', error.response.status)
59 | console.log(error.response.data)
60 | })
61 |
--------------------------------------------------------------------------------
/sample-v4.js:
--------------------------------------------------------------------------------
1 | const axios = require('axios')
2 |
3 | // An api key is emailed to you when you sign up to a plan
4 | // Get a free API key at https://api.the-odds-api.com/
5 | const apiKey = process.argv[2] || 'YOUR_API_KEY'
6 |
7 | const sportKey = 'upcoming' // use the sport_key from the /sports endpoint below, or use 'upcoming' to see the next 8 games across all sports
8 |
9 | const regions = 'us' // uk | us | eu | au. Multiple can be specified if comma delimited
10 |
11 | const markets = 'h2h' // h2h | spreads | totals. Multiple can be specified if comma delimited
12 |
13 | const oddsFormat = 'decimal' // decimal | american
14 |
15 | const dateFormat = 'iso' // iso | unix
16 |
17 | /*
18 | First get a list of in-season sports
19 | the sport 'key' from the response can be used to get odds in the next request
20 |
21 | */
22 | axios.get('https://api.the-odds-api.com/v4/sports', {
23 | params: {
24 | apiKey
25 | }
26 | })
27 | .then(response => {
28 | console.log(response.data)
29 | })
30 | .catch(error => {
31 | console.log('Error status', error.response.status)
32 | console.log(error.response.data)
33 | })
34 |
35 |
36 | /*
37 | Now get a list of live & upcoming games for the sport you want, along with odds for different bookmakers
38 | This will deduct from the usage quota
39 | The usage quota cost = [number of markets specified] x [number of regions specified]
40 | For examples of usage quota costs, see https://the-odds-api.com/liveapi/guides/v4/#usage-quota-costs
41 |
42 | */
43 | axios.get(`https://api.the-odds-api.com/v4/sports/${sportKey}/odds`, {
44 | params: {
45 | apiKey,
46 | regions,
47 | markets,
48 | oddsFormat,
49 | dateFormat,
50 | }
51 | })
52 | .then(response => {
53 | // response.data.data contains a list of live and
54 | // upcoming events and odds for different bookmakers.
55 | // Events are ordered by start time (live events are first)
56 | console.log(JSON.stringify(response.data))
57 |
58 | // Check your usage
59 | console.log('Remaining requests',response.headers['x-requests-remaining'])
60 | console.log('Used requests',response.headers['x-requests-used'])
61 |
62 | })
63 | .catch(error => {
64 | console.log('Error status', error.response.status)
65 | console.log(error.response.data)
66 | })
67 |
--------------------------------------------------------------------------------