To use Madgic's API services, you'll need to generate an API key from your dashboard. This key is used to authenticate your requests and track your usage for billing purposes.
Your API key should be kept confidential and secure. Do not share your key publicly or commit it to public repositories.
Madgic offers two primary methods to integrate with our ad service: through the Model Context Protocol (MCP) or through our REST API. MCP is recommended for most use cases, as it provides a more seamless integration with AI applications. for chat-based applications, we recommend using API, before returning the response to the user. for example:
// Generate the response from the model
const response = await model.generateText({
prompt: 'Recommend a nice hotel in Italy',
});
// Add ads to the response
const responseWithAds = await api.integrateAds(response);
// Return the response to the user
return responseWithAds;
The Model Context Protocol allows for a more seamless integration with AI applications using a streaming communication model.
Endpoint:
https://mcp.madgic.ai/mcp
Headers:
{
"mcpServers": {
"madgic-mcp": {
"command": "npx",
"args": [
"mcp-remote",
"https://mcp.madgic.ai/mcp",
"--header",
"Authorization: Bearer YOUR_API_KEY"
]
}
}
}
Make sure to include in the system prompt of the model, that would ask for ads when applicable.
Our REST API allows you to integrate ads into your content programmatically. We provide two main endpoints:
This endpoint returns relevant ads based on your content without integrating them into the text.
Endpoint:
POST https://adserver.madgic.ai/api/ads/contextual
Headers:
Authorization: Bearer YOUR_API_KEY
Request Body:
{
"context": "Your content text that needs relevant ads",
"adCount": 2 // Optional, defaults to 2. Max 3.
}
Response:
{
"status": "success | error",
"message": "error message, if any",
"data": [
{
"phrase": "Ad phrase",
"link": "http://adserver.madgic.ai/link",
"description": "Ad description"
}
]
}
This endpoint analyzes your content and returns the same content with relevant ads integrated into it.
Endpoint:
POST https://adserver.madgic.ai/api/ads/integrate
Headers:
Authorization: Bearer YOUR_API_KEY
Request Body:
{
"text": "Your content that needs ad integration",
"adCount": 2 // Optional, defaults to 2. Max 3.
}
Response:
{
"status": "success | error",
"message": "error message, if any",
"data": "Your content with integrated ads"
}
For optimal results, consider these best practices:
Provide sufficient content context to ensure relevant ad matching
Implement proper error handling in your integration
Monitor your dashboard for performance metrics and revenue tracking
Consider caching strategies to optimize performance while respecting ad freshness
If you encounter any issues or have questions about the API integration, please contact our support team through your dashboard or email support@madgic.ai.