# Problems with hitting your API using Node.js

**URL:** <https://meta.discourse.org/t/problems-with-hitting-your-api-using-node-js/146242>\
**Category:** Development\
**Created:** [March 31, 2020, 8:38am UTC](https://meta.discourse.org/t/problems-with-hitting-your-api-using-node-js/146242 "2020-03-31T08:38:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Konrad\_Sopala](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/konrad_sopala/32/167014_2.png) [@Konrad\_Sopala](https://meta.discourse.org/u/Konrad_Sopala)\
**Post date:** [March 31, 2020, 8:38am UTC](https://meta.discourse.org/t/problems-with-hitting-your-api-using-node-js/146242/1 "2020-03-31T08:38:26Z")

</div>

Hey everyone!

Not sure what category is right for it, feel free to re-categorize it. To begin with thanks for help with the Python script doing the same thing as the script in Node that I’m currently writing 👏🏼. So I’m trying to make the API request to execute the [Data Explorer](https://meta.discourse.org/t/32566?silent=true) Query receive the response and simply display the number that I get from it. While my Python code is working. It’s here:

```plaintext
# Imports

import requests
import json

# Environment Variables

ENDPOINT = 'https://community.myCompanyName.com/admin/plugins/explorer/queries/73/run'
API_KEY = '<api_key>'
API_USERNAME = '<my_username'

# Core Functions

def send_request(endpoint):

    headers = {'Content-Type': 'multipart/form-data', 'Api-Key': API_KEY, 'Api-Username': API_USERNAME}
    request = requests.post(url = endpoint, headers = headers)
    print("Request Status Code: {}".format(request.status_code))

    # Unprocessed API request response

    response = json.loads(request.text)

    response_columns = response["columns"]
    response_rows = response["rows"]

    new_users = response_rows[0][0]
    active_users = response_rows[0][1]
    new_topics = response_rows[0][2]
    employees_replies = response_rows[0][4]
    external_users_replies = response_rows[0][3] - response_rows[0][4]

    response_text = "🤖 Discourse Last Month Stats\nNew Users: {}\nActive Users: {}\nNew Topics: {}\nEmployees Replies: {}\nExternal Users Replies: {}".format(new_users, active_users, new_topics, employees_replies, external_users_replies)

    return response_text

processed_response = send_request(ENDPOINT)

```

I tried to port it to Node.js but receiving 301 status code - “Moved Permanently” and it seems to be doing exactly the same what my Python code. Here it is:

```plaintext
const http = require('http');

const options = {
  hostname: 'community.myCompanyNamee.com',
  path: '/admin/plugins/explorer/queries/73/run',
  method: 'POST',
  headers: {
    'Content-Type': 'multipart/form-data',
    'Api-Key': <api_key>,
    'Api-Username': <my_username>
  }
};

var response = 1;

const req = http.request(options, (res) => {
  console.log(`statusCode: ${res.statusCode}`);
  response = res;

  res.on('data', (d) => {
    process.stdout.write(d);
  });
});

req.on('error', (error) => {
  console.error(error);
});

req.end();

var parsed_response = JSON.parse(response);

```

I have no experience in JS so if anyone can take a look at it it would be great 🙂

---

<div class="post-metadata">

**Author:** ![Konrad\_Sopala](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/konrad_sopala/32/167014_2.png) [@Konrad\_Sopala](https://meta.discourse.org/u/Konrad_Sopala)\
**Post date:** [March 31, 2020, 11:59am UTC](https://meta.discourse.org/t/problems-with-hitting-your-api-using-node-js/146242/2 "2020-03-31T11:59:43Z")

</div>

Figured it out. Here’s the solution code:

```plaintext

const http = require('https');

const options = {
  hostname: 'community.myCompanyName.com',
  path: '/admin/plugins/explorer/queries/73/run',
  method: 'POST',
  headers: {
    accept: 'application/json',
    'Content-Type': 'multipart/form-data',
    'Api-Key': '<api_key>',
    'Api-Username': '<my_username>'
  }
};

var response = 1;

const req = http.request(options, (res) => {
  console.log(`statusCode: ${res.statusCode}`);
  response = res;

  res.on('data', (d) => {
    process.stdout.write(d);
  });
});

req.on('error', (error) => {
  console.error(error);
});

req.end();

var parsed_response = JSON.parse(response);
console.log(parsed_response);

```
