422 Error - Failed to create usage charge

Highlighted
New Member
4 0 0

Our application was working fine last 2 years but now suddenly having an issue regarding Shopify Usage charge API. As we are using functionality in our app that costs 3 cents on every user request. When a customer uses that functionality, we create usage charge associated with recurring application charge by sending a POST request to UsageCharge API as following: 

POST /admin/recurring_application_charges/#{recurring_application_charge_id}/usage_charges.json

The post body is:

{
    "usage_charge": {
        "description": "3 cents per calculation",
        "price": 0.03
    }
}

After making the post request to Shopify UsagareCharge API it returns an error 

Failed to create usage charge. The status code is 422. 

It should return the response with the used and remaining balance, like

HTTP/1.1 201 Created
{
  "usage_charge": {
    "id": 1034618222,
    "description": "Super Mega Plan 1000 emails",
    "price": "1.00",
    "created_at": "2019-10-16T16:00:37-04:00",
    "billing_on": "2018-09-04",
    "balance_used": 0.0,
    "balance_remaining": 100.0,
    "risk_level": 0
  }
}

 but it is returning the error as following:

422 Unprocessable Entity
{ "errors": { "base": [ "Failed to create usage charge" ] } }

Please advise, What is going wrong? Is Shopify API unstable nowadays?

0 Likes
Shopify Staff
Shopify Staff
240 33 37

Hey @arslan_iftikhar,

 

Can you please provide a request ID from the response headers?

0 Likes
New Member
4 0 0

Hello @scottydont

Here is the request ID from the response header: 9c076c5a-63e3-4624-8677-94e377a83863

 

0 Likes
Tourist
16 0 2

Just found this post. I'm also experiencing the same issue here: https://community.shopify.com/c/Shopify-APIs-SDKs/Creating-Usage-Charges-has-suddenly-stopped-workin...

 

It was working fine before the shop changed their plan

0 Likes
New Member
5 0 0

We are not getting 422 but we are getting 400.

 

POST failed on https://<key>:<secret>@wdevstore.myshopify.com/admin/api/2019-10/recurring_application_charges/#11930206268/usage_charges.json, 
response=HTTP/1.1 400 Bad Request, {"errors":{"recurring_application_charge":"Required parameter missing or invalid"}}

We have verified that recurring app charge exists using

GET https://<key>:<secret>@wdevstore.myshopify.com/admin/api/2019-10/recurring_application_charges/#11930206268.json

 

 

0 Likes
New Member
4 0 0

You're using post request to create the usage charge, so, I think you're missing the post body (post data) for the request. Be sure to add the post body for the request in the valid syntax (JSON). Which will look like

 

{
  "usage_charge": {
    "description": "Super Mega Plan 1000 emails",
    "price": 1.0
  }
}

You can find the detailed docs here.

 

One thing more, you also need to make sure the Content-Type header is correctly set to application/json.

 

If you still face the problem then reply with the headers and post body (DON'T include password).

0 Likes
New Member
4 0 0

My problem solved automatically after the recurring application billing cycle. Shopify support said that they have an issue open on their end to address the inconstancies with the amounts used when querying the API. There are a couple of things that need to be fixed on their end for sure.

0 Likes
New Member
5 0 0

@arslan_iftikhar Sorry I forgot to add body. Content Type is also application/json

 

{"usage_charge":{"description":"description for usage charge","price":"0.99","created_at":"2019-12-05T06:57:52.383-08:00","recurring_application_charge_id":11930206268}}

I have tried with and without recurring_application_charge_id in request body since from documentation it is not clear that is required however the Usage Charge Properties table shows it. 

 

 

0 Likes
New Member
5 0 0

It was a different issue. Shopify doc uses # in path when it comes to id of an entity in context.

 

/#{recurring_application_charge_id}/

 

# is NOT needed. They should document as following instead

 

/{recurring_application_charge_id}/

 

I was able to activate a recurring application charge and verify that it is activated. Then I tried to create usage charge against that activated charge and now it gives 403 Forbidden. Not sure if this is related to using test:true flag, in other words, am I getting 403 because corresponding recurring charge has test:true?

 

 

0 Likes