Implementing RapidAPI in React Native application

I’m having trouble integrating RapidAPI into my React Native app. Previously I was fetching user information directly from a JSON endpoint on my server and everything worked fine:

async loadUserData() {
  try {
    const result = await fetch('myserver.com/userinfo.json');
    const jsonData = await result.json();
    this.setState({
      loading: false,
      userData: jsonData,
    });
  }
  catch (err) {
    console.log(err);
  }
}

Now I want to use RapidAPI for better security but my app shows a blank screen. Here’s what I tried:

async loadUserData() {
  try {
    const result = await fetch('random-quotes1.p.rapidapi.com/quotes', {
      "method": "GET",
      "headers": {
        "x-rapidapi-host": "myserver.com",
        "x-rapidapi-key": 'abc123def456ghi789jkl012mno345pqr678stu901vwx234'
      }
    });
    const jsonData = await result.json();
    this.setState({
      loading: false,
      userData: jsonData,
    });
  }
  catch (err) {
    console.log(err);
  }
}

The RapidAPI docs show this example:

const getData = () => {
  beginRequest()
  setIsLoading(true);
  fetch('https://random-quotes1.p.rapidapi.com/quotes', {
    "method": "GET",
    "headers": {
      "x-rapidapi-host": "random-quotes1.p.rapidapi.com",
      "x-rapidapi-key": 'your-api-key'
    }
  })
  .then((result) => result.json())
  .then((response) => setQuoteData(response.quote))
  .catch(() => Alert.alert('Error occurred', 'Could not retrieve quote data.'))
  .finally(() => {
    setIsLoading(false)
    finishRequest()
  });
};

What am I doing wrong with the RapidAPI integration?

The main issue I see is in your headers configuration. You’re setting the x-rapidapi-host to “myserver.com” but it should match the actual RapidAPI endpoint you’re calling. In your case, it should be “random-quotes1.p.rapidapi.com”. Also notice you’re missing the protocol in your fetch URL - it should be “https://random-quotes1.p.rapidapi.com/quotes”. I had similar problems when I first started with RapidAPI and these header mismatches caused silent failures. Check your console logs as well, the error might be getting caught but not displayed properly. Make sure your API key is valid and has the right permissions for that specific endpoint too.

Looking at your code, there’s another issue beyond the headers that others mentioned. You’re trying to store the API response in userData state but the RapidAPI quotes endpoint returns a different data structure than your original userinfo.json. The quotes API likely returns an array or object with quote-specific fields, not user data fields. This mismatch could cause your components to render incorrectly or show blank screens even if the API call succeeds. I’d suggest console logging the actual response structure first to see what data you’re getting back, then adjust your state handling accordingly. Also double check that your component’s render method can handle the new data format from RapidAPI versus your previous JSON structure.

you’re mixing up the host header - should be "x-rapidapi-host": "random-quotes1.p.rapidapi.com" not your server domain. also missing the https:// in your fetch url which causes issues sometimes