Upload and maintain data files in Qlik Cloud

Overview

In this tutorial, you are going to learn how to create API requests that you can use to upload, update, and list data files in a Qlik Cloud tenant using JavaScript.

Requirements

  • A licensed Qlik Cloud tenant with a valid API key.
  • Node.js (version 10 or newer).
  • A modern web browser, for example, Google Chrome.
  • A text editor or IDE of your choice, for example, Visual Studio Code.

Supported data files

The following file extensions are supported:

csv, dat, fix, htm, html, kml, log, php, prn, qvd, qvo, qvs, qvx, skv, tab, txt, xls, xlsm, xlsx, xlw, xml

Sections

Upload a data file

Uploading an on-premises data file to Qlik cloud requires a series of API requests to create a space, get the connection ID of the space, and lastly, upload the file to that space.

Create a space

Data sources for apps are stored in spaces in the tenant. If you already have a space where you want the data file to reside, you can skip this section; otherwise, you need to create a space. By default, if a space is not specified, the data file will be uploaded to your personal space.

Here is an example that shows how to create a shared space using the

Spaces API with the POST method. The name of the space and the type of space is passed in the body of the request. If you want to use this example, be sure to replace the tenant URL, API key, and space name with your own values.
var request = require('request');
var options = {
  'method': 'POST',
  'url': 'https://your-tenant.us.qlikcloud.com/api/v1/spaces',
  'headers': {
    'Accept': 'application/json',

    'Authorization': 'Bearer <API_key>'
  },
  body: '{"name":"<space_name>", "type": "shared"}'};
};
request(options, function (error, response, body) {
    if (error) throw new Error(error);
    var json = JSON.parse(body);
    console.log(json);
});

The response should look something like this:


  id: '624edb82bac56c25f59f35f3',
  type: 'shared',
  ownerId: 'kqJvzqkdKhBfWc6aoSe_vQmHkcvipDkV',
  tenantId: 'oZcMPa_1PwH4FUhgaMdrh6839YHHdEJN',
  name: '<space_name>',
  description: '',
  meta: {
    actions: [ 'create', 'delete', 'read', 'update' ],
    roles: [],
    assignableRoles: [
      'consumer',
      'dataconsumer',
      'facilitator',
      'operator',
      'producer',
      'publisher'
    ]
  },
  links: {
    self: {
      href: 'https://your-tenant.us.qlik-stage.com/api/v1/spaces/624edb82bac56c25f59f35f3'
    },
    assignments: {
      href: 'https://your-tenant.us.qlik-stage.com/api/v1/spaces/624edb82bac56c25f59f35f3/assignments'
    }
  },
  createdAt: '2022-04-07T12:39:30.061Z',
  createdBy: 'kqJvzqkdKhBfWc6aoSe_vQmHkcvipDkV',
  updatedAt: '2022-04-07T12:39:30.061Z'
}

Take note of the space ID as it will be needed to find the connection ID of the space. In this example, the space ID is 624edb82bac56c25f59f35f3.

Get the connection ID

A data files connection allows you to read data from or write data to a specific space. To upload a data file to a space, you need to know the ID of the data files connection to that space, which is linked to the space ID. To get the connection ID, use the data-files API with the GET method and append the space ID to the URL as a query parameter.

  ...
  'method': 'GET',
  'url': 'https://your-tenant.us.qlik-stage.com/api/v1/data-files/connections?spaceId=624edb82bac56c25f59f35f3',
  ...

The response should look something like this:

{
  data: [
    {
      id: 'b2632d0d-8f14-483d-92d1-28bb4a086cea',
      name: 'DataFiles',
      type: 'qix-datafiles.exe',
      spaceId: '624edb82bac56c25f59f35f3'
    }
  ],
  links: {
    next: {},
    self: {
      href: 'https://your-tenant.us.qlik-stage.com:443/api/v1/data-files/connections?spaceId=624edb82bac56c25f59f35f3'
    },
    prev: {}
  }
}

From the response in the example, the connection ID is b2632d0d-8f14-483d-92d1-28bb4a086cea. You will send this ID with the file upload request.

Upload a new data file

To upload a new data file to a space, use the data-files API with the POST method. (To update an existing file, you would use the PUT method instead.)

Note: If the file size is greater than 500 MB, you should upload the file using the

Temporary contents API.

Add the filename and path of the local data file to the body of the request using the multipart/form-data format. You also need to pass the destination filename and connection ID in the body of the request, as shown in the following example.

  var request = require('request');
  var fs = require('fs');
  var options = {
  'method': 'POST',
  'url': 'https://your-tenant.us.qlikcloud.com/api/v1/data-files',
  ...
  formData: {
    'File': {
      'value': fs.createReadStream(<local_file>),
      'options': { }
    },
     'Json':'{"name": "<destination_file_name>", "connectionId": "0c3a67b4-4b66-43ea-9613-0044bbecddae"}'
  ...

The response should look something like this:

{
  id: '6de042e7-b74a-4faa-b386-b110219990ce',
  name: '<destination_file_name>',
  size: 9765,
  createdDate: '2022-04-08T15:43:13.7177587Z',
  modifiedDate: '2022-04-08T15:43:13.9490917Z',
  spaceId: '624f49f2be61041c93d600f4',
  ownerId: 'kqJvzqkdKhBfWc6aoSe_vQmHkcvipDkV'
}

The response indicates that the data file has been successfully uploaded to the tenant in the designated space.

Get a list of data files in a space

To get a list of files that reside in a given space, you need to start with the space ID.

Get the space ID

To get the space ID, pass the name of the space as a query parameter in a

Spaces API request. If you want to use this example, be sure to replace the tenant URL, API key, and space name with your own values.
var request = require('request');
var options = {
  'method': 'GET',
  'url': 'https://your-tenant.us.qlikcloud.com/api/v1/spaces?name=<space_name>,
  'headers': {
    'Accept': 'application/json',

    'Authorization': 'Bearer <API_key>'
  },
  body: '{"name":"<space_name>", "type": "shared"}'};
};
request(options, function (error, response, body) {
    if (error) throw new Error(error);
    var json = JSON.parse(body);
    console.log(json);
});

The response should look something like this:

{
  data: [
    {
      id: '624b0d1313cea76896da1ea3',
      type: 'shared',
      ownerId: 'kqJvzqkdKhBfWc6aoSe_vQmHkcvipDkV',
      tenantId: 'oZcMPa_1PwH4FUhgaMdrh6839YHHdEJN',
      name: '<space_name>',
      description: '',
      meta: [Object],
      links: [Object],
      createdAt: '2022-04-04T15:21:55.837Z',
      createdBy: 'kqJvzqkdKhBfWc6aoSe_vQmHkcvipDkV',
      updatedAt: '2022-04-04T15:21:55.837Z'
    }
  ],
  meta: { count: 1 },
  links: {
    self: {
      href: 'https://your-tenant.us.qlik-stage.com/api/v1/spaces?name=<space_name>'
    }
  }
}

From the response in the example, the space ID is 624b0d1313cea76896da1ea3. Take note of this ID because you will need it to get the connection ID of the space.

Get the connection ID of the space

Now that you've got the space ID, you can use it to get the connection ID of the space, which is used by the data-files API to identify the space. Append the space ID to the URL as a query parameter in the request.

  ...
  'method': 'GET',
  'url': 'https://your-tenant.us.qlik-stage.com/api/v1/data-files/connections?SpaceId=624b0d1313cea76896da1ea3,
  ...

The response should look something like this:

{
  data: [
    {
      id: 'c5263c30-38eb-45ba-a60c-fc708bf444a0',
      name: 'DataFiles',
      type: 'qix-datafiles.exe',
      spaceId: '624b0d1313cea76896da1ea3'
    }
  ],
  links: {
    next: {},
    self: {
      href: 'https://your-tenant.us.qlik-stage.com:443/api/v1/data-files/connections?spaceId=624b0d1313cea76896da1ea3'
    },
    prev: {}
  }
}

From the response, the connection ID is c5263c30-38eb-45ba-a60c-fc708bf444a0. You will send this ID with the next request to get the list of files in the space.

Get the list of data files in the space

Now that you've got the connection ID, you are all set to get the list of data files residing in the space. Pass the connection ID as a query parameter in a data-files API request.

  ...
  'method': 'GET',
  'url': 'https://your-tenant.us.qlik-stage.com/api/v1/data-files?connectionId=c5263c30-38eb-45ba-a60c-fc708bf444a0,
  ...

The response should look something like this:

{
  data: [
    {
      id: '51e238a0-5611-4598-89ae-10b1e957bcc5',
      name: '<file_name>',
      size: 41,
      createdDate: '2022-04-13T14:22:56.443Z',
      modifiedDate: '2022-04-13T14:22:56.61Z',
      spaceId: '624b0d1313cea76896da1ea3',
      ownerId: 'kqJvzqkdKhBfWc6aoSe_vQmHkcvipDkV'
    }
  ],
  links: {
    next: {},
    self: {
      href: 'https://your-tenant.us.qlik-stage.com:443/api/v1/data-files?connectionId=c5263c30-38eb-45ba-a60c-fc708bf444a0'
    },
    prev: {}
  }
}

From the response, you can see that there is only one file (identified by file ID 51e238a0-5611-4598-89ae-10b1e957bcc5) residing in the space.

Update the content of an existing data file

To update an existing data file in a space, you need to know its file ID.

Get the file ID

To get the file ID, pass the name of the file as a query parameter in a data-files API request. If you want to use this example, be sure to replace the tenant URL, API key, and filename with your own values.

var request = require('request');
var options = {
  'method': 'GET',
  'url': 'https://your-tenant.us.qlikcloud.com/api/v1/data-files?name=<file_name>,
  'headers': {
    'Accept': 'application/json',

    'Authorization': 'Bearer <API_key>'
  }
};
request(options, function (error, response, body) {
    if (error) throw new Error(error);
    var json = JSON.parse(body);
    console.log(json);
});

The response should look something like this:

{
  data: [
    {
      id: '431276e7-d833-41b6-b16f-1de05aed8ee6',
      name: '<file_name>',
      size: 41,
      createdDate: '2022-04-11T18:18:00.229Z',
      modifiedDate: '2022-04-11T19:33:51.103Z',
      ownerId: 'kqJvzqkdKhBfWc6aoSe_vQmHkcvipDkV'
    }
  ],
  links: {
    next: {},
    self: {
      href: 'https://your-tenant.us.qlik-stage.com:443/api/v1/data-files?name=<file_name>'
    },
    prev: {}
  }
}

From the response, the file ID is 431276e7-d833-41b6-b16f-1de05aed8ee6. You will send this ID with the next request to update the file in the space.

Update the data file

To update a file in a space, use the PUT method and pass the ID of the file as a path parameter in a data-files API request. Add the filename and path of the local updated data file to the body of the request using the multipart/form-data format, as shown in the following example.

var request = require('request');
var fs = require('fs');
var options = {
  method: 'PUT',
  url: 'https://your-tenant.us.qlik-stage.com/api/v1/data-files/431276e7-d833-41b6-b16f-1de05aed8ee6',
  headers: {
    Authorization: 'Bearer <API_key>',
  },
  formData: {
    File: {
      value: fs.createReadStream('<local_file>'),
      options: {},
    },
  },
};
request(options, function (error, response, body) {
  if (error) throw new Error(error);
  var json = JSON.parse(body);
  console.log(json);
});

The response should look something like this:

{
  id: '431276e7-d833-41b6-b16f-1de05aed8ee6',
  name: '<destination_file_name>',
  size: 41,
  createdDate: '2022-04-11T18:18:00.229Z',
  modifiedDate: '2022-04-14T13:15:28.3275041Z',
  ownerId: 'kqJvzqkdKhBfWc6aoSe_vQmHkcvipDkV'
}

The response indicates that the data file in the space has been successfully updated.

Move a data file to a different space

To move a data file to a different space, you need to know the file ID, and the space ID and connection ID of the space you want to move the file to.

Get the ID of the space

To get the ID of the space where you want to move the file to, pass the name of the space as a query parameter in a Spaces API request.

   ...
  'method': 'GET',
  'url': 'https://your-tenant.us.qlikcloud.com/api/v1/spaces?name=<space_name>,
  ...

The response should look something like this:

{
  data: [
    {
      id: '624b0d1313cea76896da1ea3',
      type: 'shared',
      ownerId: 'kqJvzqkdKhBfWc6aoSe_vQmHkcvipDkV',
      tenantId: 'oZcMPa_1PwH4FUhgaMdrh6839YHHdEJN',
      name: '<space_name>',
      description: '',
      meta: [Object],
      links: [Object],
      createdAt: '2022-04-04T15:21:55.837Z',
      createdBy: 'kqJvzqkdKhBfWc6aoSe_vQmHkcvipDkV',
      updatedAt: '2022-04-04T15:21:55.837Z'
    }
  ],
  meta: { count: 1 },
  links: {
    self: {
      href: 'https://your-tenant.us.qlik-stage.com/api/v1/spaces?name=<space_name>'
    }
  }
}

From the response, the space ID is 624b0d1313cea76896da1ea3. You will send this ID with the next API request.

Get ID of the connection to the space

Now that you've got the space ID, you can use it to get the connection ID of the space, which is used by the data-files API to identify the space. Append the space ID to the URL as a query parameter in the request.

  ...
  'method': 'GET',
  'url': 'https://your-tenant.us.qlik-stage.com/api/v1/data-files/connections?SpaceId=624b0d1313cea76896da1ea3,
  ...

The response should look something like this:

{
  data: [
    {
      id: 'c5263c30-38eb-45ba-a60c-fc708bf444a0',
      name: 'DataFiles',
      type: 'qix-datafiles.exe',
      spaceId: '624b0d1313cea76896da1ea3'
    }
  ],
  links: {
    next: {},
    self: {
      href: 'https://your-tenant.us.qlik-stage.com:443/api/v1/data-files/connections?spaceId=624b0d1313cea76896da1ea3'
    },
    prev: {}
  }
}

From the response, the connection ID is c5263c30-38eb-45ba-a60c-fc708bf444a0. Take note of this ID because you will need it later when you send the API request to move the data file to a different space.

Get the ID of the file

To get the file ID, pass the name of the file and the connection ID as query parameters in a data-files API request.

  ...
  'method': 'GET',
  'url': 'https://your-tenant.us.qlikcloud.com/api/v1/data-files?name=<file_name>&connectionId=86c11c41-994a-4149-ab09-f7734b033bde,
  ...

The response should look something like this:

{
  data: [
    {
      id: '431276e7-d833-41b6-b16f-1de05aed8ee6',
      name: '<file_name>',
      size: 41,
      createdDate: '2022-04-11T18:18:00.229Z',
      modifiedDate: '2022-04-11T19:33:51.103Z',
      ownerId: 'kqJvzqkdKhBfWc6aoSe_vQmHkcvipDkV'
    }
  ],
  links: {
    next: {},
    self: {
      href: 'https://your-tenant.us.qlik-stage.com:443/api/v1/data-files?name=<file_name>'
    },
    prev: {}
  }
}

From the response, the file ID is 431276e7-d833-41b6-b16f-1de05aed8ee6. You will send this ID and the connection ID with the next API request to move the file to a different space.

Move the file to a different space

To move the file to a different space, you need to pass the connection ID and file ID in a data-files API request. Pass the file ID as a path parameter and the connection ID as a parameter in the body of the request.

var request = require('request');
var options = {
  'method': 'PUT',
  'url': 'https://your-tenant.us.qlik-stage.com/api/v1/data-files/431276e7-d833-41b6-b16f-1de05aed8ee6',
  'headers': {
    'Authorization': 'Bearer <API_key>'
  },
  'Json': '{"connectionId": "c5263c30-38eb-45ba-a60c-fc708bf444a0"}'
  }
};
request(options, function (error, response, body) {
  if (error) throw new Error(error);
  var json = JSON.parse(body);
    console.log(json);
  });

The response should look something like this:

{
  id: '810e612f-d85d-4a40-aea2-44a35546d4d7',
  name: '<file_name>',
  size: 9765,
  createdDate: '2022-04-08T15:40:39.632Z',
  modifiedDate: '2022-04-18T15:09:10.6782592Z',
  spaceId: '624b0d1313cea76896da1ea3',
  ownerId: 'kqJvzqkdKhBfWc6aoSe_vQmHkcvipDkV'
}

The response indicates that the data file has been successfully moved to a different space.

Was this page helpful?