Notion API request returns empty response despite correct credentials

I’m working with the Notion API to fetch content from my workspace page. My setup includes proper authentication headers and the correct page ID, but when I run my Swift code, nothing prints to the console. The URLSession request seems to execute without throwing errors, but I’m not getting any visible output.

import Foundation

struct NotionPage: Codable {
    let object: String
    let id: String
}

let pageURL = URL(string: "https://api.notion.com/v1/pages/your-page-id-here")
let authToken = "secret_your_token_here"

func fetchNotionData() {
    guard let url = pageURL else { return }
    
    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
        
        if let error = requestError {
            print("Network error occurred: \(error.localizedDescription)")
            return
        }
        
        guard let httpResponse = urlResponse as? HTTPURLResponse else {
            print("Invalid response type")
            return
        }
        
        print("Status code: \(httpResponse.statusCode)")
        
        if let data = responseData {
            if let jsonString = String(data: data, encoding: .utf8) {
                print("Raw response: \(jsonString)")
            }
            
            do {
                let decodedPage = try JSONDecoder().decode(NotionPage.self, from: data)
                print("Page ID: \(decodedPage.id)")
            } catch {
                print("JSON parsing failed: \(error)")
            }
        }
    }
    
    dataTask.resume()
}

fetchNotionData()

I’ve double-checked my integration token permissions and page access rights. The request should work according to the official API docs but I’m not seeing any console output. Has anyone encountered similar issues with Notion API calls in Swift?

Yeah, you’re running this in a playground or command line, right? Your program’s ending before the async request finishes. Quick test - add sleep(5) after calling fetchNotionData() and you’ll see the output. For a real fix, use DispatchGroup or switch to async/await if you’re on iOS 15+.

No console output usually means your code’s dying before the URLSession task finishes. Since you’re making an async network call, the main thread probably exits before the completion handler runs. Add a RunLoop or semaphore to keep it alive. I hit this exact problem testing Notion API calls in a Swift script. Just add RunLoop.main.run() after your function call, or wrap it in proper async context. Also double-check your page ID format - needs to be the 32-character UUID without dashes, not the full URL path. Notion’s API is picky about ID formatting and returns empty responses for malformed IDs even with valid auth.

Same thing happened to me with Notion API in Swift. Your API setup’s fine - the program just exits before the network call finishes. I fixed it with a DispatchSemaphore to block the main thread until you get a response. Add let semaphore = DispatchSemaphore(value: 0) before your dataTask, then call semaphore.signal() at the end of your completion handler, and semaphore.wait() after dataTask.resume(). Makes the program wait for the network response. Your NotionPage struct’s probably too minimal too - Notion sends back way more fields, so make properties optional or add more fields to avoid decoding errors.