Integrating RapidAPI with React Native application

I’m having trouble implementing RapidAPI in my React Native app. Previously, I had working code that fetched user information from a JSON endpoint:

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

Now I want to switch to RapidAPI for better security, but my updated code shows a blank screen:

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

The RapidAPI documentation shows 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((data) => setQuoteData(data.quote))
  .catch(() => Alert.alert('Error occurred', 'Failed to retrieve quote data.'))
  .finally(() => {
    setIsLoading(false)
    finishRequest()
  });
};

What am I doing wrong with the RapidAPI integration?

your x-rapidapi-host header’s wrong - you’ve got “mysite.com” but it needs to be “random-quotes1.p.rapidapi.com” to match the actual endpoint. also double-check your api key’s valid and the url has https://

Your headers config is wrong. You’ve got “x-rapidapi-host”: “mysite.com” but it needs to match your actual endpoint: “x-rapidapi-host”: “random-quotes1.p.rapidapi.com”. You’re also missing https:// in your fetch URL - should be https://random-quotes1.p.rapidapi.com/quotes, not just random-quotes1.p.rapidapi.com/quotes. I ran into the same thing switching from direct JSON to RapidAPI last year. Check that your API key has permissions for that endpoint in your RapidAPI dashboard too. Log the error in your catch block - if you’re getting 401 or 403 responses, that’ll confirm it’s an auth problem.

Your endpoint URL and host header don’t match. You’re fetching from ‘random-quotes1.p.rapidapi.com/quotes’ but your host header says ‘mysite.com’ - fix that first. You’re also missing https:// in the URL, which will break the request.

I ran into the same thing when switching to RapidAPI. That blank screen means your catch block is failing silently. Add console.log(‘Full error:’, err) to see what’s actually happening - you’ll know if it’s a network issue or auth problem from the response code.

Also double-check your API key is still active in the RapidAPI dashboard and you haven’t hit your quota limit.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.