Just did this on a page. The custom code block is very strictly to logged in users so the code can’t be seen by just anyone…
<script>
// Set the <title>
const setPageTitle = (t) => (document.title = t);
// Read ?recordId=... from the URL
const params = new URLSearchParams(location.search);
const recordId = params.get("recordId");
// Softr Tables API config
const DATABASE_ID = "YOUR DATABASE ID";
const TABLE_ID = "YOUR TABLE ID";
const API_KEY = "YOUR API KEY"; // ⚠️ make sure this block is a secure page!
if (!recordId) {
console.error("No recordId in URL");
} else {
fetch(
`https://tables-api.softr.io/api/v1/databases/${DATABASE_ID}/tables/${TABLE_ID}/records/${recordId}`,
{
method: "GET",
headers: {
"Softr-Api-Key": API_KEY,
Accept: "application/json",
},
}
)
.then(async (res) => {
if (!res.ok) throw new Error(`Tables API ${res.status}: ${await res.text()}`);
return res.json();
})
.then((json) => {
const fields = json?.data?.fields || {};
const title = fields["YOUR FIELD ID TO USE"]; // Job Identifier field ID
if (title) {
setPageTitle(title);
} else {
console.warn("Field ID not found on record. Fields:", fields);
}
})
.catch((err) => console.error("Fetch error:", err));
}
</script>
My question is this though. While this actually works is there a better way to do this as I’m still a titchy bit ill at ease having that API key in the code.
Indeed . There would be a way to use a script without exposing your API Key - which is super dangerous - even with your logged in users.
Though, just one question: the field you want to use to display a dynamic page title would appear in the page itself somewhere? Like inside an item-details block? (I guess I’m referring to the Job Identifier ID).
The get-record method allows you to work with record fields of an item-details block without exposing API keys or unnecessary database details.
The idea is the following: by using the get-record event method, you can create a script to update the document title.
Note that you may need to add:
window.addEventListener('block-loaded-ITEM_DETAILS_BLOCK_ID', () => {
// rest of your script nested here
});
Replace ITEM_DETAILS_BLOCK_ID with the actual item-details block ID.
The reason is that you may need to ensure the item-details block is available before the rest of the script runs.
To be added in the page settings, custom code, header.
item-details1 to be changed (twice) with your own item-details block id
In my case, I’m looking for the field “Name” (to be replaced line 3 and 4 - case sensitive)
If your field has spaces you can’t use the dot notation (.Name)
So it would look like this, using bracket notation: