# Create influxdb (type: flux) datasource using API with token

**URL:** <https://community.grafana.com/t/create-influxdb-type-flux-datasource-using-api-with-token/45950>\
**Category:** InfluxDB\
**Tags:** api\
**Created:** [April 12, 2021, 11:32pm UTC](https://community.grafana.com/t/create-influxdb-type-flux-datasource-using-api-with-token/45950 "2021-04-12T23:32:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![grafanauser](https://avatars.discourse-cdn.com/v4/letter/g/d07c76/32.png) [@grafanauser](https://community.grafana.com/u/grafanauser)\
**Post date:** [April 12, 2021, 11:32pm UTC](https://community.grafana.com/t/create-influxdb-type-flux-datasource-using-api-with-token/45950/1 "2021-04-12T23:32:06Z")

</div>

I am trying to add a new datasource using API and need to to be scripted. I can’t find a way to set the “token” in the InfluxDB details field.

I’ve looked and the following [documentation](https://grafana.com/docs/grafana/latest/http_api/data_source/) and able to create a new influxdb datasource except the token, see snapshot below.

![image](https://us1.discourse-cdn.com/grafana/original/2X/1/1065dbfdb9273dd4352ba8a225005531b8d672a6.png)

The curl command below works but looking from the gui, obviously “Token” field is blank. How do I set the token field using api?

curl -X POST --insecure “Authorization: Bearer \<MY\_TOKEN\>” -H “Content-Type: application/json” -d ‘{  
“name”:“InfluxDB”,  
“type”:“influxdb”,  
“typeName”:“InfluxDB”,  
“access”:“proxy”,  
“url”:“http://MY\_INFLUXDB:8086”,  
“basicAuth”:false,  
“jsonData”: {  
“defaultBucket”:“MY\_BUCKET”,  
“httpMode”:“POST”,  
“organization”:“MY\_ORG”,  
“version”:“Flux”  
},  
“readOnly”:false  
}’ [http://admin:admin@community.grafana.com/api/datasources](http://admin:admin@community.grafana.com/api/datasources)

---

<div class="post-metadata">

**Author:** ![lgki](https://avatars.discourse-cdn.com/v4/letter/l/6a8cbe/32.png) [@lgki](https://community.grafana.com/u/lgki)\
**Post date:** [June 17, 2021, 12:51pm UTC](https://community.grafana.com/t/create-influxdb-type-flux-datasource-using-api-with-token/45950/2 "2021-06-17T12:51:56Z")

</div>

Here is an example of my python script, “\<\>” are my variables that you have to replace with yours

```auto
import requests
import json

apiKey1 = "<MyApiKey>"
token = "<MyInfluxDBToken>"

server = "http://grafana.staged-by-discourse.com/"
url = server + "api/datasources/"
headers = {"Authorization":"Bearer "+apiKey1,
    "Content-Type":"application/json",
    "Accept":"application/json"
}

my_json = {
    "orgId":1,
    "name":"InfluxDB",
    "type":"influxdb",
    "typeLogoUrl":"",
    "access":"proxy",
    "url":"http://localhost:8086",
    "password":"",
    "user":"",
    "database":"",
    "basicAuth":False,
    "basicAuthUser":"",
    "basicAuthPassword":"",
    "withCredentials":False,
    "isDefault":False,
    "jsonData":{
        "defaultBucket":"test_bucket",
        "httpMode":"POST",
        "organization":"<OrgName>",
        "version":"Flux"
    },
    "secureJsonData":{
        "token": token
    },
    "version":2,
    "readOnly":False
}

r = requests.post(url = url.format('/api/datasources'), headers = headers, json = my_json, verify=False)

data = r.json()
print(data)

```

To see what you need (because it depends on your version) you can see the POST request with a valid datasource and using the GUI. In dev tools record the network to see the request.

When you get it, you can delete `id`, `uid` and replace `secureJsonField` with `secureJsonData`.  
**Your token have to be in** `secureJsonData`.

---

<div class="post-metadata">

**Author:** ![rikutozumoko](https://avatars.discourse-cdn.com/v4/letter/r/4af34b/32.png) [@rikutozumoko](https://community.grafana.com/u/rikutozumoko)\
**Post date:** [November 3, 2023, 11:41am UTC](https://community.grafana.com/t/create-influxdb-type-flux-datasource-using-api-with-token/45950/3 "2023-11-03T11:41:55Z")

</div>

I tried this and it works fine,  
But I have 2 organizations with OrgID:2, I tried to change OrgId to 2, but the datasources was still added on my first organization with OrgId:1.  
Have you experienced this?

---

<div class="post-metadata">

**Author:** ![yosiasz](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/yosiasz/32/45847_2.png) [@yosiasz](https://community.grafana.com/u/yosiasz)\
**Post date:** [November 3, 2023, 12:16pm UTC](https://community.grafana.com/t/create-influxdb-type-flux-datasource-using-api-with-token/45950/4 "2023-11-03T12:16:14Z")

</div>

Are you sure the second organization’s org id is 2?

Make sure to always add try catch to your code

---

<div class="post-metadata">

**Author:** ![rikutozumoko](https://avatars.discourse-cdn.com/v4/letter/r/4af34b/32.png) [@rikutozumoko](https://community.grafana.com/u/rikutozumoko)\
**Post date:** [November 3, 2023, 2:11pm UTC](https://community.grafana.com/t/create-influxdb-type-flux-datasource-using-api-with-token/45950/5 "2023-11-03T14:11:57Z")

</div>

I get it, OrgId is assigned automatically based on Token as per documentation. So it seems like it doesn’t important in JSON anymore.
