phb71
1
Hello
I’m not sure if this is possible or not with Netlify.
I want to run a cron job through Netlify to send an email to every Netlify user in Identity, once a day. I believe I could by:
- fetching all users and their email addresses
- running a Netlify scheduled function for each of them.
However, I couldn’t find a way to fetch all users using gotrue. Is it possible?
If not, I think I’ll need to use an external DB to store user’s emails - any recommended one for Netlify builds?
Paul
You could use the Netlify API:
phb71
3
I’ve checked the API docs and that other ticket and I didn’t find anything that allows you to request the full list of users.
This is what you’re looking for:
phb71
5
Good find - I missed it. Thank you
Now the issue is that I can’t get context.clientContext to return anything in my Netlify function in my local environment - why?
After I deploy it to prod, the error above disappeared but then I get a different error that res.json() isn’t a function - why?
Code:
const axios = require('axios')
exports.handler = async (event, context) => {
const { identity, user } = context.clientContext
const usersUrl = `${identity.url}/admin/users`
const adminAuthHeader = 'Bearer ' + identity.token
console.log('event', JSON.stringify(event))
console.log('context', JSON.stringify(context))
let data
try {
data = await axios.get(usersUrl, {
headers: {
Authorization: adminAuthHeader
}
}).then((res) => res.json())
} catch (e) {
return {
statusCode: 500,
body: JSON.stringify({
error: e.message
})
}
}
return {
statusCode: 200,
body: JSON.stringify(data)
}
}
Because of this:
You can get that if you use a custom JWT secret in site settings and in your netlify.toml
for the CLI, but that’s a Business+ feature, I believe.
You’re using axios
not fetch
. Axios itself converts the data to json. You can get it using: res.data
.
1 Like
Glad to know everything is working. Happy building