Follow the steps below to set up your page after generating the API key:
Once you have your unique API Key, you'll need to include it in your webpage so it can fetch SEO data dynamically. Here’s how:
<head> section of your HTML:<meta name="token-key" content="YOUR_API_KEY_HERE">
YOUR_API_KEY_HERE with the API Key you generated.Next, add the JavaScript code that will fetch the SEO data based on the token key and update the page dynamically:
<head> section of your HTML:
<script>
// Fetch SEO Data from the API
const tokenKey = document.querySelector('meta[name="token-key"]').getAttribute('content');
if (!tokenKey) {
console.error("Token key not found in the meta tag.");
} else {
fetch('https://www.jaysoftware.co.uk/af/a_seo_ai/api/get-seo-code/', {
method: 'POST', // Ensure POST method is used
headers: {
'Content-Type': 'application/json' // Content type for JSON
},
body: JSON.stringify({ token: tokenKey }) // Send the token in the request body
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
if (data.error) {
console.error("Error fetching SEO data:", data.error);
return;
}
if (data.title) {
document.title = data.title;
}
if (data.description) {
let metaDescription = document.querySelector('meta[name="description"]');
if (!metaDescription) {
metaDescription = document.createElement('meta');
metaDescription.name = 'description';
document.head.appendChild(metaDescription);
}
metaDescription.setAttribute('content', data.description);
}
if (data.keywords) {
let metaKeywords = document.querySelector('meta[name="keywords"]');
if (!metaKeywords) {
metaKeywords = document.createElement('meta');
metaKeywords.name = 'keywords';
document.head.appendChild(metaKeywords);
}
metaKeywords.setAttribute('content', data.keywords);
}
if (data.canonical_url) {
let canonicalLink = document.querySelector('link[rel="canonical"]');
if (!canonicalLink) {
canonicalLink = document.createElement('link');
canonicalLink.rel = 'canonical';
document.head.appendChild(canonicalLink);
}
canonicalLink.href = data.canonical_url;
}
if (data.schema_markup) {
let schemaScript = document.createElement('script');
schemaScript.type = 'application/ld+json';
schemaScript.textContent = data.schema_markup;
document.head.appendChild(schemaScript);
}
})
.catch(error => console.error("Fetch error:", error));
}
</script>
The SEO data will automatically update the following elements in your webpage:
<meta> tags for description, keywords, and the <link> tag for the canonical URL. You should also see a <script> tag for schema markup.If you encounter any issues:
F12 and go to the Console tab).
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SEO Page Setup</title>
<meta name="description" content="">
<meta name="keywords" content="">
<meta name="token-key" content="YOUR_API_KEY_HERE">
<script>
// Add the JavaScript for fetching SEO data here...
</script>
</head>
<body>
<h1>Welcome to Your SEO Optimized Page!</h1>
<p>This page’s SEO data is dynamically loaded based on your API Key.</p>
</body>
</html>