How do I get the connection string for a database through the API?
Hey there! I am testing out using Neon and my usecase requires using the API To create a database which is all good.
I am struggling to find how to generate/get the connection string required to connect to the created database.
Is there an endpoint to fetch it?
8 Replies
automatic-azure•2y ago
Hey @Amr!
Since you're creating databases via the API, you specify the project ID, the branch ID, database name and owner.
Each Neon branch has its own connection string which includes the database you're trying to connect to:
postgresql://neondb_owner:<password>@ep-morning-hall-a5fqgk7v.us-east-2.aws.neon.tech/neondb?sslmode=require
Here you're connecting to the database called neondb, to connect to a different database you just need to change the neondb value
e.g. postgresql://neondb_owner:<password>@ep-morning-hall-a5fqgk7v.us-east-2.aws.neon.tech/<database_name>?sslmode=require
Does this answer your question?quickest-silverOP•2y ago
Thank you, it partially answers it. The problem is I still need to somehow know the password and host. Both are Neon generated values that I cam read and hardcode from the console but I am wondering if there's a better way of fetching this info form the API on-demand.
Right now, I am making a call to /endpoints to get the host. So just stuck on the password part.
automatic-azure•2y ago
So the host should remain unchanged in the case of creating a database. It will be different if you're creating branches however.
As for the password, it's actually related to the database owner/role. So you should be able to connect using one of the roles you have available (neondb_owner is the default role)
So in this example, the host , role and password are still the same
Do you mind sharing more details about what you're building? Happy to point you in the right direction
stuck-chocolate•2y ago
The API has an endpoint to fetch the connection string. It will include the credentials https://api-docs.neon.tech/reference/getconnectionuri
Neon
Get a connection URI
Retrieves a connection URI for the specified database.<br/>You can obtain a <button aria-label="Copy Code" class="rdmd-code-copy fa"></button>project_id by listing the projects for your Neon account.<br/>You can obtain the <button aria-label="Copy Code" class="rdmd-code-co...
quickest-silverOP•2y ago
Perfect, thank you both for the help. I am essentially building a mobile app builder. Each app will have its own database. When a request is received, the API needs to pull the relevant connection string based on which app issued the request.
stuck-chocolate•2y ago
Interesting. I'm actually looking into this idea of database "fleets" too. I'd like to hear more about your use case and architecture - got a few minutes to chat? I can DM you and arrange something @Amr
quickest-silverOP•2y ago
Happy to chat @ShinyPokemon . I am free in a couple of hours for ~20-30 minutes. Feel free to DM me we can schedule some other day.
stuck-chocolate•2y ago
@Amr DM'd