Getting 403 forbidden error when calling Yahoo Finance API via RapidAPI in Swift

I’m working on a personal iOS app and need to get stock market data from Yahoo Finance through RapidAPI. I signed up for their free tier and received my API credentials.

Here’s the Swift code I’m using to fetch company information:

import Foundation

let apiHeaders = [
    "x-rapidapi-host": "yahoo-finance3.p.rapidapi.com",
    "x-rapidapi-key": "your-api-key-here"
]

let apiURL = "https://yahoo-finance3.p.rapidapi.com/v1/finance/result?region=US&lang=en&ticker=TSLA"
let urlRequest = NSMutableURLRequest(url: URL(string: apiURL)!,
                                   cachePolicy: .useProtocolCachePolicy,
                                   timeoutInterval: 15.0)
urlRequest.httpMethod = "GET"
urlRequest.allHTTPHeaderFields = apiHeaders

let urlSession = URLSession.shared
let task = urlSession.dataTask(with: urlRequest as URLRequest) { (responseData, urlResponse, requestError) in
    if let error = requestError {
        print("Request failed: \(error)")
    } else {
        if let httpResp = urlResponse as? HTTPURLResponse {
            print("Status code: \(httpResp.statusCode)")
        }
    }
}

task.resume()

Every time I run this I get a 403 forbidden response. Other RapidAPI endpoints work perfectly with similar code patterns. The Yahoo Finance API appears to be active since it works in their online testing tool. What could be causing this authorization issue?

Check your host header - you’ve got “yahoo-finance3.p.rapidapi.com” but different Yahoo Finance APIs use different hostnames. It needs to match exactly what’s in the RapidAPI docs for your specific provider. I’ve made this mistake before - copied code from one Yahoo Finance API while subscribed to another with a slightly different hostname. Try adding “Content-Type”: “application/json” to your headers too, even for GET requests. Some APIs are picky. Still getting 403s? Test with a different stock symbol - some tickers are restricted based on your subscription level.

Had the same problem with Yahoo Finance through RapidAPI. You probably need to actually subscribe to the endpoint in your RapidAPI dashboard - even for the free tier. Just having credentials isn’t enough. Go to the API page and hit subscribe. Also check your endpoint URL format. Different Yahoo Finance providers on RapidAPI use slightly different URLs and parameters. Make sure you’re copying the exact format from their docs. And verify your API key hasn’t hit rate limits - sometimes 403 errors mean you’ve maxed out your quota, not that your auth is broken.

hey, make sure your yahoo finance api key is valid. sometimes, rapidapi needs you to enable the api manually even if you’re on free tier. also, try adding a user-agent header, cause some apis block requests without it.