Fetching from the API
Making Requests
Section titled “Making Requests”The RESTasaurus API can be accessed using various tools and programming languages. Below are examples for common use cases.
Base URL
Section titled “Base URL”https://restasaurus.onrender.com/api/v1Examples
Section titled “Examples”# Get all dinosaurs (first page)curl https://restasaurus.onrender.com/api/v1/dinosaurs?page=1
# Get dinosaur by namecurl https://restasaurus.onrender.com/api/v1/dinosaurs?name=Tyrannosaurus
# Get dinosaurs by dietcurl https://restasaurus.onrender.com/api/v1/dinosaurs?diet=CarnivoreJavaScript
Section titled “JavaScript”// Using fetchasync function fetchDinosaurs() { try { const response = await fetch( 'https://restasaurus.onrender.com/api/v1/dinosaurs?page=1' );
if (!response.ok) { throw new Error(`Failed to fetch dinosaurs with status: ${response.status}`); }
const data = await response.json(); console.log('Successfully fetched dinosaurs'); return data; } catch (error) { console.error('Failed to fetch dinosaurs:', error); throw error; }}// Using axiosimport axios from 'axios';
async function fetchDinosaurs() { try { const response = await axios.get( 'https://restasaurus.onrender.com/api/v1/dinosaurs?page=1' ); console.log('Successfully fetched dinosaurs'); return response.data; } catch (error) { console.error('Failed to fetch dinosaurs:', error.message); throw error; }}Python
Section titled “Python”import requests
# Using requests def fetch_dinosaurs(): try: response = requests.get('https://restasaurus.onrender.com/api/v1/dinosaurs?page=1') response.raise_for_status() print('Successfully fetched dinosaurs') return response.json() except requests.exceptions.RequestException as error: print(f'Failed to fetch dinosaurs: {error}') raiseImportant Note About Response Time
Section titled “Important Note About Response Time”Warming Up the API
Section titled “Warming Up the API”If you need to ensure the API is responsive before making requests, you can “warm it up” by pinging the /dinosaurs?page=1 endpoint:
# Warm up the API and ensure it is running before making other requestscurl https://restasaurus.onrender.com/api/v1/dinosaurs?page=1This will trigger the service to wake up, ensuring subsequent requests are faster.
Next Steps
Section titled “Next Steps”- Explore the API Endpoints documentation to learn about all available endpoints
- Check out the OpenAPI Specification for detailed API documentation
- Review the Model Overview to understand the data structure