1
curl -n -X GET https://api.heroku.com/apps/mypgapp/addons/heroku-postgresql
-H "Content-Type: application/json"   
-H "Accept: application/vnd.heroku+json; version=3"

Returns a JSON with reelevant PG add-on information, but does not provide the connection strings as the CLI provides with the pg:credentials command. What I want to get via Heroku REST API is the host, port, dbname, user and password.

Is that possible?

panchicore
  • 11,451
  • 12
  • 74
  • 100

1 Answers1

2

I've found it can be quite useful to look at the debug output of the Heroku CLI. In this case, you can get the query string from the environment variables:

DEBUG=1 HEROKU_DEBUG=1 heroku config:get -a mypgapp DATABASE_URL                                                   ❖ ruby-2.3.0
heroku-cli/5.2.12-09f3ecc (darwin-amd64) go1.6.2 /Users/bjeanes/.local/share/heroku/cli/bin/heroku cmd: version
heroku-cli/5.2.12-09f3ecc (darwin-amd64) go1.6.2 /Users/bjeanes/.local/share/heroku/cli/bin/heroku cmd: commands
heroku-cli/5.2.12-09f3ecc (darwin-amd64) go1.6.2 /Users/bjeanes/.local/share/heroku/cli/bin/heroku cmd: config:get
--> GET /apps/mypgapp/config-vars
<-- 200 OK
<-- {"DATABASE_URL":"postgres://REDACTED@ec2-174-129-223-35.compute-1.amazonaws.com:5432/dfbe4e6vs4diqb"}
postgres://REDACTED@ec2-174-129-223-35.compute-1.amazonaws.com:5432/dfbe4e6vs4diqb

So, you can then translate that into a curl request and get the value out using jq:

$ curl -n -H 'Accept: application/vnd.heroku+json; version=3' \
    https://api.heroku.com/apps/mypgapp/config-vars | jq .DATABASE_URL
postgres://REDACTED@ec2-174-129-223-35.compute-1.amazonaws.com:5432/dfbe4e6vs4diqb

This is not the API that heroku pg:credentials uses. It actually used Heroku's own deprecated v2 API.

Bo Jeanes
  • 6,294
  • 4
  • 42
  • 39