Solved

Subscriber get returning a 401


Badge +2

When I test the /subscribers/{app_user_id} endpoint on the API reference page using my public key, I get a 200 response.

However, when I run the same code from my Node cloud function, I consistently get back a 401.

Any ideas as to why this is? 

icon

Best answer by David Krebs 3 May 2022, 15:39

View original

4 replies

Userlevel 5
Badge +9

Can you share the error message you’re getting along with the 401? Are you authenticating correctly with the authorization header and `Bearer <YOUR_API_KEY>`

Badge +2

Yes, certainly.

I am setting the Authorization header with my api key using the “Bearer <API_KEY>” format.

Also, I checked the url, and it’s good.

When I print the error object, I get a status of “401” and a statusText of “Unauthorized”.

What is returned is the following object:

{ timeout: 0, size: 0 }

 

Badge +2

I discovered a workaround.

 

This code gives me the 401

const sdk = require('api')('@revenuecat/v4.0#fawmbsl21aqc0w');

sdk.subscribers({
app_user_id: 'app_user_id',
Authorization: 'Bearer <API_KEY>'
})
.then(res => console.log(res))
.catch(err => console.error(err));

 

If I instead use axios, I get the happy response

const axe = axios.create({
baseURL: 'https://api.revenuecat.com/v1/subscribers',
headers: {
'Authorization': 'Bearer <API_KEY>'
}
});

axe.get('/app_user_id')
.then((response) => console.log(response.data))
.catch(() => console.log(e));

 

Badge

The Node snippet in the documentation didn’t work for me either, but I was able to get it to work by letting the api package handle the authorization header:

 

const sdk = require('api')('@revenuecat/v4.0#fawmbsl21aqc0w');    

sdk.auth('<API_KEY>')
sdk.subscribers({app_user_id: `app_user_id`})
.then(res => console.log(res))
.catch(err => console.error(err))

 

Reply