It’s not possible to get member id if member is not logged-in yet. Which make sense but some applications are “visitor ready” and can be used without any account.
But as I said it’s not possible to get a member id/visitor id with any API in Wix. But there is a way to create a custom API that will work with Wix’s system to get user’s visitor ids that’s actually generated by Wix and used in Wix Apps!
How?
Few days ago I found something while working with “http-functions” (your own custom REST APIs). Let me show you;
In http-functions we have a parameter in functions which is req
this parameter contains info about the request that made to our API.
And inside of that req
parameter we have a property called headers
which contains request headers. And we can actually access visitor id from this headers
prop that’s located in req
.
Quick test:
Go to your http-functions.js file and add this code:
import { ok, response } from 'wix-http-functions';
export async function get_currentVisitorId(req) {
try {
return ok({
body: JSON.parse(req.headers.identities)
});
} catch (err) {
throw Error(`Failed to get current visitor id: ${err}`);
}
}
This code will return something like this:
{
"identificationData": {
"context": {
"metasiteId": "ff4848ad-7003-4d29-8549-67e943acf832",
"visitorId": "1edde3e1-c4ec-45ab-8986-ba3c40148e5d"
},
"identities": [
{
"person": {
"type": "VISITOR",
"id": "1edde3e1-c4ec-45ab-8986-ba3c40148e5d"
}
}
]
},
"signedToken": "IDN2.eyJraWQiOiJwXzhJbHFzLSIsImFsZyI6IlJTMjU2In0.eyJkYXRhIjoie1wiY29udGV4dFwiOntcIm1ldGFzaXRlSWRcIjpcImZmNDg0OGFkLTcwMDMtNGQyOS04NTQ5LTY3ZTk0M2FjZjgzMlwiLFwidmlzaXRvcklkXCI6XCIxZWRkZTNlMS1jNGVjLTQ1YWItODk4Ni1iYTNjNDAxNDhlNWRcIn0sXCJpZGVudGl0aWVzXCI6W3tcInBlcnNvblwiOntcInR5cGVcIjpcIlZJU0lUT1JcIixcImlkXCI6XCIxZWRkZTNlMS1jNGVjLTQ1YWItODk4Ni1iYTNjNDAxNDhlNWRcIn19XX0iLCJpYXQiOjE3MTE0MDM0MDksImV4cCI6MTcxMzk5NTQwOX0.EvUHh_LnPbeK9tLjEL50BgvocRD7_xmTXLUcM5Ykw-NfE55KjOI-gYa_iQQ72JawaBVuEa-HJH7a--T2bvoVvTxVRhPgo9l0y82xTdB8kFcM_qQQI2IMALHk5LkYa_m0lFSLm58OSqp7uiWhNO3bhB0KHFgHaCPnVXdk3oxW_Ph4d1I-5-Yw2dhM0o3XktoJn8DxYrBttv7zsBb82ip_n0I_8D1wmPE_bmUY8TTuOkmikAYlGKatB9jMK2itgg0JmSrA19DAN21yA83hGZmg7gHYCLifgO-BZ67t21cW7CelZA90hfEIUmuItEX0IeRTDfMAz51ZPP6FCRabcR-rgg"
}
Boom! We got the current visitor id or if user is logged in you can also get the current member id from this API response.
Demo here (will be unpublished after few days)
You can get much more info from headers, using same system.
How to Use This API?
After that point when you setup the API all you need to do is call this API from the frontend and save the returned data to localStorage/sessionStorage if needed.
You can use axios or Wix’s fetch APIs for calling the API or native fetch function in JS
Example:
// IN PAGE CODE or ANY FRONTEND FILE
const res = await axios.get("your api url");
I will also build an NPM package that you can setup with single line for this that also includes visitor id and other details.