I am trying to use curl based on some variables to create customers in Stripe, but when I assign the token to a variable it is giving me an error on Stripe saying that it does not exist. However, if I put the text in directly it works. How can I use the $TOKEN variable, is there something changing the value that I don't realize?

Michael$ curl https://api.stripe.com/v1/customers -u $access_token: -d source=tok_1CjvRiDZ5DqZ0yaUVWXXXXXX
  "error": {
    "code": "token_already_used",
    "doc_url": "https://stripe.com/docs/error-codes/token-already-used",
    "message": "You cannot use a Stripe token more than once: tok_1CjvRiDZ5DqZ0yaUVWXXXXXX.",
    "type": "invalid_request_error"

Michael$ curl https://api.stripe.com/v1/customers -u $access_token: -d source=$TOKEN
  "error": {
    "code": "resource_missing",
    "doc_url": "https://stripe.com/docs/error-codes/resource-missing",
    "message": "No such token: \"tok_1CjvRiDZ5DqZ0yaUVWXXXXXX\"",
    "param": "source",
    "type": "invalid_request_error"

$TOKEN is assigned like this

OUTPUT="$(curl https://api.stripe.com/v1/tokens -u $access_token: -d customer=$external_customer_id)"
TOKEN="$(echo $OUTPUT | jq .id)"
  • 1
    How do you assign the token to the variable. Any double quotes should not be part of the value (which they would be if you escape them). – Kusalananda Jul 3 '18 at 21:15
  • I assign the token from a curl call, I will add the details – Michael St Clair Jul 3 '18 at 21:15

jq gives the output quoted, as would be appropriate for JSON.

$ echo '{ "foo": "foobar" }' | jq .foo

The quotes end up as part of the contents of TOKEN, and are not removed when the variable is expanded. (They're just ordinary characters at that point.)

However, there's an option just for this:

--raw-output / -r:

With this option, if the filter´s result is a string then it will be written directly to standard output rather than being formatted as a JSON string with quotes. This can be useful for making jq filters talk to non-JSON-based systems.

So, use TOKEN="$(echo "$OUTPUT" | jq -r .id)" instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.