I’m trying to fetch content from my Notion workspace using their REST API but getting no output in the console. My setup includes a valid endpoint URL and API token that work fine when tested elsewhere.
import Foundation
struct NotionPage: Codable {
let pageId: String
let name: String
}
let apiUrl = URL(string: "https://api.notion.com/v1/pages/abc123def456")
let authToken = "secret_xyz789"
func fetchNotionData() {
guard let url = apiUrl else { return }
let dataTask = URLSession.shared.dataTask(with: url) { responseData, urlResponse, requestError in
if let networkError = requestError {
print("Network error occurred: \(networkError)")
return
}
if let safeData = responseData {
do {
let jsonParser = JSONDecoder()
let parsedContent = try jsonParser.decode(NotionPage.self, from: safeData)
} catch {
print("JSON parsing failed")
}
}
if let status = urlResponse as? HTTPURLResponse {
if status.statusCode == 200 {
if let rawData = responseData {
print(String(data: rawData, encoding: .utf8) ?? "No data")
}
} else {
print("Bad status code: \(status.statusCode)")
}
}
}
dataTask.resume()
}
I also tried enabling network debugging through Xcode environment settings but that didn’t show any additional information. Has anyone encountered similar issues with Notion’s API endpoints?
check ur page id format - notion needs dashes in specific spots or the api will reject it. ur datatask logic is backwards too. parse the response first, then check status codes. u might be getting error json from notion that u’re missing coz of the order.
You’re missing the authentication headers for Notion’s API. Notion needs both an Authorization header with your bearer token and a Notion-Version header. Your code just creates a basic URLRequest without these, so you’re getting empty responses.
Here’s what you need to do - create a URLRequest and add the headers first:
var request = URLRequest(url: url)
request.setValue("Bearer \(authToken)", forHTTPHeaderField: "Authorization")
request.setValue("2022-06-28", forHTTPHeaderField: "Notion-Version")
let dataTask = URLSession.shared.dataTask(with: request) { responseData, urlResponse, requestError in
// your existing code here
}
I hit this same wall when I started with Notion’s API. Those auth headers aren’t optional - skip them and you’ll get 401s or empty data. Also double-check that your integration has permissions for whatever page you’re trying to access.
Yeah, it’s definitely an authentication issue, but there’s another problem with your URLSession code. You’re parsing the successful response in one block but printing raw data in a separate status check. If auth fails, you won’t see Notion’s actual error message. Move the response logging outside the status check so it prints regardless - Notion sends really helpful error messages even when requests fail. Their API tells you exactly what’s wrong: missing permissions, bad page IDs, auth problems, whatever. Also, your NotionPage struct probably doesn’t match their response format. Notion’s page objects are way more complex with tons of nested properties, so your decoder will crash even if auth works.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.