@kyanny's blog

My thoughts, my life. Views/opinions are my own.

How to call GraphQL API with curl

If you can't use a better GraphQL client than curl, you can call GraphQL API with curl and a few shell utilities, but without jq.

  • Enclose GraphQL query with double-quote and escape all double-quotes in the query because the query must be a single JSON value.
  • Remove all newline (\n) characters in the query (POST body must be a one-line JSON data).


Note: The string value of "query" must escape newline characters or the schema will not parse it correctly. For the POST body, use outer double quotes and escaped inner double quotes.


❯ cat query.txt
  license(key:"mit") {
❯ bash graphql.sh < query.txt
{"data":{"license":{"name":"MIT License","description":"A short and simple permissive license with conditions only requiring preservation of copyright and license notices. Licensed works, modifications, and larger works may be distributed under different terms and without source code."}}}