Get All Blogs
Get All Blogs Endpoint
Section titled “Get All Blogs Endpoint”Use the following endpoint to get all blog posts via the API.
Endpoint
Section titled “Endpoint”GET /api/v1/public/get-all-blogsAuthentication
Section titled “Authentication”Include your API key in the Authorization header:
Authorization: Bearer YOUR_API_KEYCode Examples
Section titled “Code Examples”// Endpoint: {API_ENDPOINTS.BLOGS.GET_ALL}fetch(`https://api.blogz.ai/api/v1/public/get-all-blogs`, { method: 'GET', // Method is GET to retrieve data headers: { 'Authorization': `Bearer YOUR_API_KEY`, 'Content-Type': 'application/json' // Good practice, though not strictly needed for GET }}).then(response => response.json()) // Expecting an ARRAY of blog objects.then(blogsArray => { console.log('All blogs retrieved successfully:', blogsArray); // Iterate over the array or use it as needed blogsArray.forEach(blog => { console.log(`- ${blog.title}`); });}).catch(error => console.error('Error retrieving all blogs:', error));import { Blog } from './your-types-path'; // Assuming you have a Blog type client-side
// Endpoint: {API_ENDPOINTS.BLOGS.GET_ALL}fetch(`https://api.blogz.ai/api/v1/public/get-all-blogs`, { method: 'GET', headers: { 'Authorization': `Bearer YOUR_API_KEY`, 'Content-Type': 'application/json' }}).then(async (response: Response) => { // Expecting an ARRAY of blog objects, cast the promise result return response.json() as Promise<Blog[]>;}).then((blogsArray: Blog[]) => { console.log('All blogs retrieved successfully:', blogsArray); // blogsArray is typed as Blog[] blogsArray.forEach((blog: Blog) => { console.log(`- ${blog.title} (${blog._id})`); });}).catch((error: Error) => console.error('Error retrieving all blogs:', error));import requestsimport json
# Endpoint: {API_ENDPOINTS.BLOGS.GET_ALL}api_url = "https://api.blogz.ai/api/v1/public/get-all-blogs"api_key = "YOUR_API_KEY"
headers = { "Authorization": f"Bearer {api_key}", "Accept": "application/json"}
try: response = requests.get(api_url, headers=headers)
if response.status_code == 200: try: # Expecting a LIST (array) of blog dictionaries blogs_list = response.json() print(f"Successfully retrieved {len(blogs_list)} blog(s):") # blogs_list is a list of dictionaries print(json.dumps(blogs_list, indent=2)) # Example: Iterate through the list # for blog in blogs_list: # print(f"- {blog.get('title', 'No Title')}") except json.JSONDecodeError: print(f"Error: Received status code 200 but failed to decode JSON response body.") print(f"Response body: {response.text}") else: response.raise_for_status()
except requests.exceptions.RequestException as e: print(f"Error retrieving all blogs: {e}") if e.response is not None: print(f"Status Code: {e.response.status_code}") try: error_details = e.response.json() print("Error details:", json.dumps(error_details, indent=2)) except json.JSONDecodeError: print(f"Response body: {e.response.text}")# Use GET method (default for curl if no -X is specified, but explicit is clearer)
# Endpoint: {API_ENDPOINTS.BLOGS.GET_ALL}curl -X GET "https://api.blogz.ai/api/v1/public/get-all-blogs" \ -H "Authorization: Bearer YOUR_API_KEY" \ -H "Accept: application/json" # Request JSON response