# how create a organization and bucket in influxdb with python

**URL:** <https://community.influxdata.com/t/how-create-a-organization-and-bucket-in-influxdb-with-python/35113>\
**Category:** Systems\
**Tags:** python\
**Created:** [July 30, 2024, 10:37am UTC](https://community.influxdata.com/t/how-create-a-organization-and-bucket-in-influxdb-with-python/35113 "2024-07-30T10:37:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ali\_sharify](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/ali_sharify/32/16038_2.png) [@ali\_sharify](https://community.influxdata.com/u/ali_sharify)\
**Post date:** [July 30, 2024, 10:37am UTC](https://community.influxdata.com/t/how-create-a-organization-and-bucket-in-influxdb-with-python/35113/1 "2024-07-30T10:37:53Z")

</div>

how create a organization and bucket in influxdb with python?  
i have a python script for creating a org and bucket in influx db but it wont work and return unauthorized response

can any body help me in this issue with influxdb apis?

```json
HTTP response body: 
{ "code": "unauthorized", "message": "write:orgs is unauthorized" }

```

Python Code:

```python
from influxdb_client import InfluxDBClient, BucketRetentionRules
from influxdb_client.rest import ApiException

def create_org_and_bucket(url, token, org_name, bucket_name, bucket_retention_days):
    # Convert retention days to seconds
    if bucket_retention_days == -1:
        bucket_retention_seconds = None # No expiration
    else:
        bucket_retention_seconds = bucket_retention_days * 86400 # Convert days to seconds

    # Connect to InfluxDB
    client = InfluxDBClient(url=url, token=token)
    orgs_api = client.organizations_api()
    buckets_api = client.buckets_api()

    try:
        # Create organization
        org = orgs_api.create_organization(name=org_name)
        print(f"Organization '{org_name}' created successfully.")

        try:
            # Create bucket
            if bucket_retention_seconds is None:
                retention_rules = []
            else:
                retention_rules = [BucketRetentionRules(type="expire", every_seconds=bucket_retention_seconds)]

            bucket = buckets_api.create_bucket(name=bucket_name, org_id=org.id, retention_rules=retention_rules)
            print(f"Bucket '{bucket_name}' in organization '{org_name}' created successfully.")
            return org, bucket

        except ApiException as e:
            # If bucket creation fails, delete the created organization
            print(f"Error creating bucket: {e}")
            orgs_api.delete_organizations_id(org.id)
            print(f"Rolled back organization '{org_name}'.")
            return False

    except ApiException as e:
        print(f"Error creating organization: {e}")
        return False

    finally:
        # Close the connection to InfluxDB
        client.close()

url = "http://localhost:8086/"
token = ""
org_name = input("org name: ")
bucket_name = input("bucket name: ")
bucket_retention_days = -1

result = create_org_and_bucket(url, token, org_name, bucket_name, bucket_retention_days)
if result:
    org, bucket = result
    print(f"Successfully created organization '{org.name}' and bucket '{bucket.name}'.")
else:
    print("Failed to create organization and bucket.")

```

i try this and also ask gpt and gemini but it wont work and returns same error

---

<div class="post-metadata">

**Author:** ![suyash](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/suyash/32/15791_2.png) [@suyash](https://community.influxdata.com/u/suyash)\
**Post date:** [July 31, 2024, 5:40pm UTC](https://community.influxdata.com/t/how-create-a-organization-and-bucket-in-influxdb-with-python/35113/2 "2024-07-31T17:40:36Z")

</div>

The error message you’re receiving suggests that the token you’re using doesn’t have the necessary permissions to create organizations. Can you create a new token with all permissions “All Access API Token” and try with that?
