401 error - company search using Node.js

Hi,

I’m running Node.js locally, I have updated my hosts file and added ‘127.0.0.1 app-test.com’ to it. I have also added http://app-test.com to my JavaScript domains in the application details.

I’m querying the API using the superagent NPM package as describe in this post - Confusing documentation for beta API - #2 by mike

The problem is that I keep getting 401 errors - the result is below.

Can anyone help me here?

    header: {},
     writable: true,
     _redirects: 0,
     _maxRedirects: 5,
     cookies: '',
     qs: { q: 'Sage' },
     qsRaw: [],
     _redirectList: [],
     req:
      ClientRequest {
        domain: null,
        _events: [Object],
        _eventsCount: 3,
        _maxListeners: undefined,
        output: [],
        outputEncodings: [],
        outputCallbacks: [],
        writable: true,
        _last: true,
        chunkedEncoding: false,
        shouldKeepAlive: false,
        useChunkedEncodingByDefault: false,
        sendDate: false,
        _removedHeader: {},
        _contentLength: 0,
        _hasBody: true,
        _trailer: '',
        finished: true,
        _headerSent: true,
        socket: [Object],
        connection: [Object],
        _header: 'GET /search/companies?q=Sage HTTP/1.1\r\nHost: api.companiesho
use.gov.uk\r\nAccept-Encoding: gzip, deflate\r\nUser-Agent: node-superagent/1.4.
0\r\nAuthorization: Basic MGNiZjEzZjQwZTIyY2U0YTBmYzQ4MDk2YWY3YTM5ZGY6\r\nConnec
tion: close\r\n\r\n',
        _headers: [Object],
        _headerNames: [Object],
        agent: [Object],
        socketPath: undefined,
        method: 'GET',
        path: '/search/companies?q=Sage',
        parser: null,
        res: [Object] },
     protocol: 'https:',
     host: 'api.companieshouse.gov.uk',
     _callback: [Function],
     res:
      IncomingMessage {
        _readableState: [Object],
        readable: false,
        domain: null,
        _events: [Object],
        _eventsCount: 4,
        _maxListeners: undefined,
        socket: [Object],
        connection: [Object],
        httpVersionMajor: 1,
        httpVersionMinor: 1,
        httpVersion: '1.1',
        complete: true,
        headers: [Object],
        rawHeaders: [Object],
        trailers: {},
        rawTrailers: [],
        upgrade: false,
        url: '',
        method: null,
        statusCode: 401,
        statusMessage: 'Unauthorized',
        client: [Object],
        _consuming: true,
        _dumped: false,
        req: [Object],
        text: '',
        read: [Function],
        body: undefined },
     response: [Circular],
     _timeout: 0,
     called: true },
  req:
   ClientRequest {
     domain: null,
     _events: { drain: [Function], error: [Function], response: [Function] },
     _eventsCount: 3,
     _maxListeners: undefined,
     output: [],
     outputEncodings: [],
     outputCallbacks: [],
     writable: true,
     _last: true,
     chunkedEncoding: false,
     shouldKeepAlive: false,
     useChunkedEncodingByDefault: false,
     sendDate: false,
     _removedHeader: {},
     _contentLength: 0,
     _hasBody: true,
     _trailer: '',
     finished: true,
     _headerSent: true,
     socket:
      TLSSocket {
        _tlsOptions: [Object],
        _secureEstablished: true,
        _securePending: false,
        _newSessionPending: false,
        _controlReleased: true,
        _SNICallback: null,
        servername: null,
        npnProtocol: undefined,
        authorized: true,
        authorizationError: null,
        encrypted: true,
        _events: [Object],
        _eventsCount: 9,
        _connecting: false,
        _hadError: false,
        _handle: null,
        _parent: null,
        _host: 'api.companieshouse.gov.uk',
        _readableState: [Object],
        readable: false,
        domain: null,
        _maxListeners: undefined,
        _writableState: [Object],
        writable: false,
        allowHalfOpen: false,
        destroyed: true,
        bytesRead: 259,
        _bytesDispatched: 227,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        ssl: [Object],
        server: undefined,
        _requestCert: true,
        _rejectUnauthorized: true,
        parser: null,
        _httpMessage: [Circular],
        read: [Function],
        _consuming: true,
        _idleNext: null,
        _idlePrev: null,
        _idleTimeout: -1 },
     connection:
      TLSSocket {
        _tlsOptions: [Object],
        _secureEstablished: true,
        _securePending: false,
        _newSessionPending: false,
        _controlReleased: true,
        _SNICallback: null,
        servername: null,
        npnProtocol: undefined,
        authorized: true,
        authorizationError: null,
        encrypted: true,
        _events: [Object],
        _eventsCount: 9,
        _connecting: false,
        _hadError: false,
        _handle: null,
        _parent: null,
        _host: 'api.companieshouse.gov.uk',
        _readableState: [Object],
        readable: false,
        domain: null,
        _maxListeners: undefined,
        _writableState: [Object],
        writable: false,
        allowHalfOpen: false,
        destroyed: true,
        bytesRead: 259,
        _bytesDispatched: 227,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        ssl: [Object],
        server: undefined,
        _requestCert: true,
        _rejectUnauthorized: true,
        parser: null,
        _httpMessage: [Circular],
        read: [Function],
        _consuming: true,
        _idleNext: null,
        _idlePrev: null,
        _idleTimeout: -1 },
     _header: 'GET /search/companies?q=Sage HTTP/1.1\r\nHost: api.companieshouse
.gov.uk\r\nAccept-Encoding: gzip, deflate\r\nUser-Agent: node-superagent/1.4.0\r
\nAuthorization: Basic MGNiZjEzZjQwZTIyY2U0YTBmYzQ4MDk2YWY3YTM5ZGY6\r\nConnectio
n: close\r\n\r\n',
     _headers:
      { host: 'api.companieshouse.gov.uk',
        'accept-encoding': 'gzip, deflate',
        'user-agent': 'node-superagent/1.4.0',
        authorization: 'Basic MGNiZjEzZjQwZTIyY2U0YTBmYzQ4MDk2YWY3YTM5ZGY6' },
     _headerNames:
      { host: 'Host',
        'accept-encoding': 'Accept-Encoding',
        'user-agent': 'User-Agent',
        authorization: 'Authorization' },
     agent:
      Agent {
        domain: null,
        _events: [Object],
        _eventsCount: 1,
        _maxListeners: undefined,
        defaultPort: 443,
        protocol: 'https:',
        options: [Object],
        requests: {},
        sockets: [Object],
        freeSockets: {},
        keepAliveMsecs: 1000,
        keepAlive: false,
        maxSockets: Infinity,
        maxFreeSockets: 256,
        maxCachedSessions: 100,
        _sessionCache: [Object] },
     socketPath: undefined,
     method: 'GET',
     path: '/search/companies?q=Sage',
     parser: null,
     res:
      IncomingMessage {
        _readableState: [Object],
        readable: false,
        domain: null,
        _events: [Object],
        _eventsCount: 4,
        _maxListeners: undefined,
        socket: [Object],
        connection: [Object],
        httpVersionMajor: 1,
        httpVersionMinor: 1,
        httpVersion: '1.1',
        complete: true,
        headers: [Object],
        rawHeaders: [Object],
        trailers: {},
        rawTrailers: [],
        upgrade: false,
        url: '',
        method: null,
        statusCode: 401,
        statusMessage: 'Unauthorized',
        client: [Object],
        _consuming: true,
        _dumped: false,
        req: [Circular],
        text: '',
        read: [Function],
        body: undefined } },
  links: {},
  text: '',
  body: {},
  files: {},
  buffered: true,
  headers:
   { 'content-length': '0',
     connection: 'close',
     date: 'Mon, 21 Sep 2015 11:40:14 GMT',
     'cache-control': 'no-store, no-cache, must-revalidate, post-check=0, pre-ch
eck=0',
     'access-control-expose-headers': 'Location,www-authenticate',
     pragma: 'no-cache' },
  header:
   { 'content-length': '0',
     connection: 'close',
     date: 'Mon, 21 Sep 2015 11:40:14 GMT',
     'cache-control': 'no-store, no-cache, must-revalidate, post-check=0, pre-ch
eck=0',
     'access-control-expose-headers': 'Location,www-authenticate',
     pragma: 'no-cache' },
  statusCode: 401,
  status: 401,
  statusType: 4,
  info: false,
  ok: false,
  redirect: false,
  clientError: true,
  serverError: false,
  error:
   { [Error: cannot GET /search/companies?q=Sage (401)]
     status: 401,
     text: '',
     method: 'GET',
     path: '/search/companies?q=Sage' },
  accepted: false,
  noContent: false,
  badRequest: false,
  unauthorized: true,
  notAcceptable: false,
  forbidden: false,
  notFound: false,
  type: '',
  setEncoding: [Function: bound ],
  redirects: [] }

Solved - there was an issue with my key.

I’m glad to hear the issue is resolved and thanks for letting us know. :smiley: