Help needed to add GraphQL API keys in LangChain script

Hi guys,

I wanna chat with the data in MongoDB :smiley: I have succeeded with data that is not stored in MongoDB.

Does anyone have experience in working with LangChain? and connecting OpenAI/ChatGPT to MongoDB with GraphQL API keys?

I have issues connecting to the database because I can’t find a way to add a header/apikey correctly so I don’t get an error 401 “401 Client Error: Unauthorized for url:…”

I have tested with a simple connect with requests, and i worked fine., json={‘query’: query}, headers=headers)

LangChain with GQL doc:

Code snippet:

API_KEY = “bdytnQMOwW…6zMZEY”
headers = {“api-key”: API_KEY}

tools = load_tools(
graphql_endpoint=“App Services”,

Hi Rasmus,

Is there a reason you’re aiming to connect via GraphQL to MongoDB Atlas instead of going directly from the MongoDB driver (pymongo) akin to what’s described in the docs here (MongoDB Atlas | 🦜️🔗 Langchain


1 Like

Hi Andrew,

Thanks for your input and link, appreciated :smiley:
Im have structured data in MDB (real estate data: sqm, address, sold price etc.), and kind of using LLM to create the GQL query and output the result from that. Prompting “What are the latest sales in x-zipcode?”.

Reviewing your link, seems like it’s for text or unstructured data or?
LLM is optimal for unstructured data, but also great for writing queries which makes it easy for non-code/tech ppl to extract data from db. I haven’t found another way/method to “ask the database” so far :smiley:

Hi @Rasmus_Gregersen
All the params in GraphQL tool in Langchain are specified here

So you are thinking of generating the GraphQL from natural language using an LLM (GPT4 works pretty well for certain cases for instance ) and then query MongoDB using that?

1 Like

Awesome, I will have a look, thanks a lot @Prakul_Agarwal :smiley:

Yep, exactly. I have made a small demo using LangChain, ChatGPT and GraphQL API (not MDB). The verbose in the output shows the generated GQL query:

1 Like

Found a solution to connect to MongoDB with GraphQL API auth.

  1. Download the LangChain package: GitHub - langchain-ai/langchain: ⚡ Building applications with LLMs through composability ⚡
  2. Edit the file so it can take a “headers” input (apiKey: “xzy”)
  3. pip install the package in your Python script/terminal

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.