# Consul namespace query

**URL:** <https://discuss.hashicorp.com/t/consul-namespace-query/32064>\
**Category:** Consul\
**Created:** [November 17, 2021, 7:46pm UTC](https://discuss.hashicorp.com/t/consul-namespace-query/32064 "2021-11-17T19:46:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rudrapbiswas](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/rudrapbiswas/32/7464_2.png) [@rudrapbiswas](https://discuss.hashicorp.com/u/rudrapbiswas)\
**Post date:** [November 17, 2021, 7:46pm UTC](https://discuss.hashicorp.com/t/consul-namespace-query/32064/1 "2021-11-17T19:46:48Z")

</div>

I have enterprise consul installed and it has multiple namespaces.  
I want to use python module to query for specified namespace. Basically if I have namespaces: ns\_1, ns\_2 … etc. I want to get the service definition and their respective meta for ns\_1, ns\_2 individually.

The input parameter would be the namespace name (ns\_1/ns\_2).  
Can someone plz help me on how to accomplish this.

–Thanks

---

<div class="post-metadata">

**Author:** ![karl-cardenas-coding](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/karl-cardenas-coding/32/19422_2.png) [@karl-cardenas-coding](https://discuss.hashicorp.com/u/karl-cardenas-coding)\
**Post date:** [November 17, 2021, 8:11pm UTC](https://discuss.hashicorp.com/t/consul-namespace-query/32064/2 "2021-11-17T20:11:37Z")

</div>

Hi @rudrapbiswas you could accomplish this with two API calls. You would first use the list services `/catalog` [API](https://www.consul.io/api-docs/catalog#list-services). This would return all the services in the specified namespace. Once you have the list of all services and their `service_id`, you can then use the get service definition [API](https://www.consul.io/api-docs/agent/service#get-service-configuration) endpoint to get the service definition and respective metadata.

There are some [community](https://www.consul.io/api-docs/libraries-and-sdks) python tools worth checking out. However, be aware that the community tools might not be up-to-date. Hope this helps.

---

<div class="post-metadata">

**Author:** ![rudrapbiswas](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/rudrapbiswas/32/7464_2.png) [@rudrapbiswas](https://discuss.hashicorp.com/u/rudrapbiswas)\
**Post date:** [November 17, 2021, 8:14pm UTC](https://discuss.hashicorp.com/t/consul-namespace-query/32064/3 "2021-11-17T20:14:48Z")

</div>

Thanks for the response.  
My first hurdle is to filter out the namespace. How can I only filter the services for a particular namespace?  
And I need to use Python’s consul module. Through carl, I am able to, but not thru Python.

–Thanks

---

<div class="post-metadata">

**Author:** ![karl-cardenas-coding](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/karl-cardenas-coding/32/19422_2.png) [@karl-cardenas-coding](https://discuss.hashicorp.com/u/karl-cardenas-coding)\
**Post date:** [November 17, 2021, 8:34pm UTC](https://discuss.hashicorp.com/t/consul-namespace-query/32064/4 "2021-11-17T20:34:18Z")

</div>

I’m not familiar with the Python Consul module. But to answer your question. The API endpoint to list services accept a query parameter for namespace. I’m assuming you already know what namespaces you have available. Unless you are asking how to get all available namespaces?

---

<div class="post-metadata">

**Author:** ![rudrapbiswas](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/rudrapbiswas/32/7464_2.png) [@rudrapbiswas](https://discuss.hashicorp.com/u/rudrapbiswas)\
**Post date:** [November 17, 2021, 8:58pm UTC](https://discuss.hashicorp.com/t/consul-namespace-query/32064/5 "2021-11-17T20:58:06Z")

</div>

Yes, you are right. I have the namespaces available to me. I just need to pass them as a parameter and get rest of the data.

–Thanks

---

<div class="post-metadata">

**Author:** ![karl-cardenas-coding](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/karl-cardenas-coding/32/19422_2.png) [@karl-cardenas-coding](https://discuss.hashicorp.com/u/karl-cardenas-coding)\
**Post date:** [November 17, 2021, 9:04pm UTC](https://discuss.hashicorp.com/t/consul-namespace-query/32064/6 "2021-11-17T21:04:29Z")

</div>

Good, then in that case you can simply use the Consul API directly with a pythons http package library. I have used [requests](https://docs.python-requests.org/en/latest/) before, and it is a popular http package library that is user friendly. As for the Python Consul module, I would take a peek at its docs and see if the namespace parameter is available for the list services method. Otherwise, you need to craft the HTTP call yourself in python.
