# Create New Datasource

Endpoint Name: `insertdatasource`

Request type : `POST`

Authorization: Bearer token as header

Endpoint Details: The `insertdatasource` endpoint will be used to create new datasource.

<table data-header-hidden><thead><tr><th width="116"></th><th></th></tr></thead><tbody><tr><td>Method</td><td>URL</td></tr><tr><td>POST</td><td><code>URL/access/klearstack/insertdatasource</code></td></tr></tbody></table>

### Request Body

<table data-header-hidden><thead><tr><th width="185"></th><th width="144"></th><th width="107.0909423828125"></th><th></th></tr></thead><tbody><tr><td>Params</td><td>Values</td><td>Required</td><td>Description</td></tr><tr><td>datasource_name</td><td>string</td><td>Yes</td><td>Name of the datasource</td></tr><tr><td>company_name</td><td>string</td><td>Yes</td><td>Name of the company for which the user is logging in</td></tr><tr><td>data</td><td>string(JSON)</td><td>Yes</td><td><p>A valid JSON string containing info about Datasource to be created. It contains info about which columns to create and datatype of those columns</p><p>Example:</p><p>{</p><p>"DataSourceName": "test ",</p><p>"DataSourceType": "excel_or_csv",</p><p>"DataSourceStatus": true,</p><p>"fields": [{</p><p>"name": "USER_FIRST_NAME",</p><p>"datatype": "string"</p><p>}, {</p><p>"name": "email",</p><p>"datatype": "string"</p><p>}]</p><p>}</p></td></tr><tr><td>dataset(file)</td><td>Binary</td><td>Yes</td><td>Excel or CSV file containing data for datasource.</td></tr></tbody></table>

### Shell (cURL)

* Request Body

```bash
curl --location "URL/access/klearstack/insertdatasource" \
  --header "Authorization: Bearer your_token_here" \
  --form "datasource_name=Replace_Datasource_Name_Here" \
  --form "company_name=Replace_Company_Name_Here" \
  --form 'data={
    "DataSourceName": "test",
    "DataSourceType": "excel_or_csv",
    "DataSourceStatus": true,
    "fields": [
      {"name": "USER_FIRST_NAME", "datatype": "string"},
      {"name": "email", "datatype": "string"}
    ]
  }' \
  --form "dataset=@/path/to/your/file.csv"
```

### Python

* Request Body

```python
import requests

url = "URL/access/klearstack/insertdatasource"

payload = {'datasource_name': 'Replace_Datasource_Name_Here',
'company_name': 'Replace_Company_Name_Here',
'data': '{
    "DataSourceName": "test",
    "DataSourceType": "excel_or_csv",
    "DataSourceStatus": true,
    "fields": [
      {"name": "USER_FIRST_NAME", "datatype": "string"},
      {"name": "email", "datatype": "string"}
    ]
  }'}
files=[
  ('dataset',('file.csv',open('/path/to/your/file.csv','rb'),'text/csv'))
]
headers = {
  'Authorization': 'Bearer your_token_here'
}

response = requests.request("POST", url, headers=headers, data=payload, files=files)

print(response.text)

```

### Java (OkHttp)

* Request Body

```java
OkHttpClient client = new OkHttpClient().newBuilder()
  .build();
MediaType mediaType = MediaType.parse("text/plain");
RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
  .addFormDataPart("datasource_name", "Replace_Your_Datasource_Name_Here")
  .addFormDataPart("company_name", "Replace_Your_Company_Name_Here")
  .addFormDataPart("data", "{\n" +
    "    \"DataSourceName\": \"Replace_Your_DataSource_Name_Here\",\n" +
    "    \"DataSourceType\": \"excel_or_csv\",\n" +
    "    \"DataSourceStatus\": true,\n" +
    "    \"fields\": [\n" +
    "      {\"name\": \"Replace_Field_Name_1\", \"datatype\": \"string\"},\n" +
    "      {\"name\": \"Replace_Field_Name_2\", \"datatype\": \"string\"}\n" +
    "    ]\n" +
    "  }")
  .addFormDataPart("dataset", "Replace_Your_File_Name.csv",
    RequestBody.create(MediaType.parse("application/octet-stream"),
    new File("Replace_Your_File_Path_Here/Replace_Your_File_Name.csv")))
  .build();
Request request = new Request.Builder()
  .url("URL/access/klearstack/insertdatasource")
  .method("POST", body)
  .addHeader("Authorization", "Bearer Replace_Your_Token_Here")
  .build();
Response response = client.newCall(request).execute();

```

### Node.js (Axios)

* Request Body

```javascript
const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs');

let formData = new FormData();
formData.append('datasource_name', 'Replace_Your_Datasource_Name_Here');
formData.append('company_name', 'Replace_Your_Company_Name_Here');
formData.append('data', '{\r\n    "DataSourceName": "Replace_Your_DataSource_Name_Here",\r\n    "DataSourceType": "excel_or_csv",\r\n    "DataSourceStatus": true,\r\n    "fields": [\r\n      {"name": "Replace_Field_Name_1", "datatype": "string"},\r\n      {"name": "Replace_Field_Name_2", "datatype": "string"}\r\n    ]\r\n  }');
formData.append('dataset', fs.createReadStream('Replace_Your_File_Path_Here/Replace_Your_File_Name.csv'));

let config = {
  method: 'post',
  maxBodyLength: Infinity,
  url: 'URL/access/klearstack/insertdatasource',
  headers: { 
    'Authorization': 'Bearer Replace_Your_Token_Here', 
    ...formData.getHeaders()
  },
  data: formData
};

axios.request(config)
  .then((response) => {
    console.log(JSON.stringify(response.data));
  })
  .catch((error) => {
    console.log(error);
  });

```

### JavaScript (XHR)

* Request Body

```javascript
// WARNING: For POST requests, body is set to null by browsers.

var formData = new FormData();
formData.append("datasource_name", "Replace_Your_Datasource_Name_Here");
formData.append("company_name", "Replace_Your_Company_Name_Here");
formData.append("data", "{\r\n    \"DataSourceName\": \"Replace_Your_DataSource_Name_Here\",\r\n    \"DataSourceType\": \"excel_or_csv\",\r\n    \"DataSourceStatus\": true,\r\n    \"fields\": [\r\n      {\"name\": \"Replace_Field_Name_1\", \"datatype\": \"string\"},\r\n      {\"name\": \"Replace_Field_Name_2\", \"datatype\": \"string\"}\r\n    ]\r\n  }");
formData.append("dataset", fileInput.files[0], "Replace_Your_File_Name.csv");

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function() {
  if(this.readyState === 4) {
    console.log(this.responseText);
  }
});

xhr.open("POST", "URL/access/klearstack/insertdatasource");
xhr.setRequestHeader("Authorization", "Bearer Replace_Your_Token_Here");

xhr.send(formData);
```

### API Response

<table data-header-hidden><thead><tr><th width="147"></th><th></th></tr></thead><tbody><tr><td>Status code</td><td>Example Response</td></tr><tr><td>200</td><td><p><code>{</code></p><p>    <code>"success": true,</code></p><p>    <code>“release_version”:”7.8.9”</code></p><p><code>}</code></p></td></tr><tr><td>400</td><td><p><code>{</code></p><p>    <code>"error": "Datasource Name already exists"</code></p><p><code>}</code></p></td></tr></tbody></table>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://klearstack.gitbook.io/klearstack-api-documentation/datasources/create-new-datasource.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
