### Create Next.js App with Apollo Server Example Source: https://docs.canopyapi.co/examples/apollo-server This command initializes a new Next.js application using a specific example repository designed for integrating with Apollo Server and the Canopy API. It sets up the project structure and necessary configurations. ```bash npx create-next-app --example https://github.com/canopy-api/canopy-api/tree/main/docs/examples/next-with-apollo-server your-application-name ``` -------------------------------- ### Create Next.js App with GraphQL Yoga Source: https://docs.canopyapi.co/examples/graphql-yoga This command initializes a new Next.js project using a template that includes GraphQL Yoga integration with the Canopy API. It sets up the basic structure for a GraphQL server within a Next.js application. ```bash npx create-next-app --example https://github.com/canopy-api/canopy-api/tree/main/docs/examples/next-with-graphql-yoga your-application-name ``` -------------------------------- ### Next.js GraphQL API Route with GraphQL Yoga Source: https://docs.canopyapi.co/examples/graphql-yoga This example shows how to create a GraphQL API route in a Next.js application using GraphQL Yoga and the `@graphql-tools/executor-yoga` package. It configures the remote executor with the Canopy API endpoint and includes the necessary Authorization header. ```typescript import{ buildHTTPExecutor }from'@graphql-tools/executor-http'import{ createYoga }from'graphql-yoga'import{ useExecutor }from'@graphql-tools/executor-yoga' constCANOPY_GRAPHQL_ENDPOINT='https://endpoint.canopyapi.co/' const remoteExecutor =buildHTTPExecutor({ endpoint:CANOPY_GRAPHQL_ENDPOINT, headers:{ Authorization:'Bearer CANOPY-API-KEY', }, }) exportconst config ={ api:{ bodyParser:false, }, } exportdefaultcreateYoga({ plugins:[useExecutor(remoteExecutor)], graphqlEndpoint:'/api/graphql', }) ``` -------------------------------- ### Search Amazon Products Source: https://docs.canopyapi.co/hoppscotch-collection Performs a search on Amazon for products based on a given search term. This query returns a list of products, each with its title and price. It requires the search term and the Canopy API key. ```graphql query AmazonSearchResults { amazonProductSearchResults(input:{searchTerm:"ipad"}) { productResults { results { title price { display } } } } } ``` -------------------------------- ### Fetch Amazon Product Details (US) Source: https://docs.canopyapi.co/hoppscotch-collection Retrieves detailed information for a specific Amazon product using its ASIN. This query fetches the product's title, brand, image URL, review counts, rating, and price. It requires the product's ASIN and the Canopy API key. ```graphql query amazonProduct { amazonProduct(input: { asinLookup: {asin: "B0BDHWDR12"} }) { __typename title brand mainImageUrl reviewsTotal ratingsTotal rating price { display } } } ``` -------------------------------- ### Fetch Amazon Product Details (UK) Source: https://docs.canopyapi.co/hoppscotch-collection Fetches detailed information for an Amazon product in the UK market by its ASIN. Similar to the US version, this query retrieves title, brand, image, reviews, rating, and price. It requires the ASIN and the Canopy API key, specifying the UK domain. ```graphql query amazonProduct { amazonProduct(input: { asinLookup: {asin: "B0BDJ37NF5", domain: UK} }) { __typename title brand mainImageUrl reviewsTotal ratingsTotal rating price { display } } } ``` -------------------------------- ### Python REST API Client Source: https://docs.canopyapi.co/contributing/how-to-contribute Provides an example of how to make requests to the CanopyAPI's REST endpoints using Python. This is useful for backend services or scripts written in Python. ```Python import requests response = requests.get('https://api.canopyapi.co/rest/v1/resource') print(response.json()) ``` -------------------------------- ### Get Amazon Products by Category Source: https://docs.canopyapi.co/hoppscotch-collection Fetches products belonging to a specific Amazon category, identified by its category ID. The query returns the category's ID, name, and a list of products within that category, including their titles. It requires the category ID and the Canopy API key. ```graphql query AmazonProductCategory { amazonProductCategory(input: {categoryId:"4991426011"}) { id name productResults { results { title } } } } ``` -------------------------------- ### Fetch Amazon Product Data (JavaScript) Source: https://docs.canopyapi.co/examples/rest-fetch This snippet demonstrates how to use the browser's fetch API to query the CanopyAPI REST API for Amazon product details. It includes setting the API key in the request header and handling the response. It's intended for testing and proof-of-concept scenarios due to API key exposure concerns in production. ```JavaScript const asin ='B0B3JBVDYP'; const apiKey =''; const response = await fetch(`https://rest.canopyapi.co/api/amazon/product?asin=${asin}&domain=US`, { method: 'GET', headers: { 'API-KEY': apiKey, 'Content-Type': 'application/json' } }); if (response.ok) { const data = await response.json(); console.log(data); } else { console.error('Request failed:', response.status); } ``` -------------------------------- ### Get Amazon Category Taxonomy Source: https://docs.canopyapi.co/hoppscotch-collection Retrieves the hierarchical structure of Amazon product categories. This query returns a list of category names, allowing exploration of the available categories. It requires the Canopy API key. ```graphql query AmazonCategoryTaxonomy { amazonProductCategoryTaxonomy { name } } ``` -------------------------------- ### Query Amazon Product Data with JavaScript Fetch Source: https://docs.canopyapi.co/examples/fetch This snippet shows how to use the browser's fetch API to send a GraphQL query to the CanopyAPI endpoint. It retrieves details for a specific Amazon product using its ASIN. Remember to replace '' with your actual API key and be cautious about exposing it in production. ```JavaScript const query =` query amazonProduct { amazonProduct(input: {asin: "B0B3JBVDYP"}) { title mainImageUrl rating price { display } } } ` const response =awaitfetch('https://graphql.canopyapi.co/',{method:'POST',mode:'cors',headers:{'Content-Type':'application/json','API-KEY':'',},body:JSON.stringify({query}),})return response.json() ``` -------------------------------- ### GraphQL API Route Example with Apollo Server Stitching Source: https://docs.canopyapi.co/examples/apollo-server This example shows how to create a GraphQL API route in a Next.js application using Apollo Server. It utilizes `@graphql-tools/stitch` to combine the Canopy API schema with your existing GraphQL schema, requiring the inclusion of the `Authorization` header for API access. ```typescript import{ ApolloServer }from'@apollo/server' import{ startServerAndCreateNextHandler }from'@as-integrations/next' import{ buildHTTPExecutor }from'@graphql-tools/executor-http' import{ schemaFromExecutor }from'@graphql-tools/wrap' import{ stitchSchemas }from'@graphql-tools/stitch' constCANOPY_GRAPHQL_ENDPOINT='https://endpoint.canopyapi.co/' const remoteExecutor =buildHTTPExecutor({ endpoint:CANOPY_GRAPHQL_ENDPOINT, headers:{ Authorization:'Bearer CANOPY-API-KEY', }, }) const canopySubschema ={ schema:awaitschemaFromExecutor(remoteExecutor), executor: remoteExecutor, } const schema =stitchSchemas({ subschemas:[canopySubschema], }) const apolloServer =newApolloServer({ schema }) exportdefaultstartServerAndCreateNextHandler(apolloServer) ``` -------------------------------- ### Python GraphQL API Source: https://docs.canopyapi.co/index Illustrates how to send GraphQL queries to the Canopy API using Python. This example uses the 'requests' library and requires an API key for authentication. ```Python import requests api_key = 'YOUR_API_KEY' url = 'https://graphql.canopyapi.co/' headers = { 'Content-Type': 'application/json', 'API-KEY': api_key # or 'Authorization': f'Bearer {api_key}' } query = ''' { products(query: "example") { title price } } ''' response = requests.post(url, headers=headers, json={'query': query}) if response.status_code == 200: print(response.json()) else: print(f"Error: {response.status_code}") ``` -------------------------------- ### Configure Authorization Header in Next.js Source: https://docs.canopyapi.co/examples/graphql-yoga This code snippet demonstrates how to set the Authorization header in a Next.js application when using GraphQL Yoga to connect to the Canopy API. It shows where to replace the placeholder API key in the `pages/api/graphql.ts` file. ```typescript const remoteExecutor =buildHTTPExecutor({... headers:{ Authorization:"Bearer CANOPY-API-KEY",},}); ``` -------------------------------- ### Fetch Amazon Product Data via REST API (Python) Source: https://docs.canopyapi.co/examples/rest-python A Python example using the 'requests' library to query the Canopy REST API for Amazon product data. It demonstrates how to set up the endpoint URL, parameters (like ASIN and domain), and headers including an API key. The code handles successful responses by printing JSON data and logs errors for failed requests. ```python import requests # Define the REST API endpoint url ='https://rest.canopyapi.co/api/amazon/product' # Define the parameters params ={'asin':'B0B3JBVDYP','domain':'US'} # Define the headers headers ={'API-KEY':'','Content-Type':'application/json'} # Send the GET request to the REST API endpoint response = requests.get(url, params=params, headers=headers) if response.status_code ==200: data = response.json() print(data) else: print(f"Request failed with status code: {response.status_code}") print(f"Response: {response.text}") ``` -------------------------------- ### Python REST API Source: https://docs.canopyapi.co/index Provides an example of how to interact with the Canopy API's REST endpoint using Python. It utilizes the 'requests' library and requires an API key for authentication. ```Python import requests api_key = 'YOUR_API_KEY' url = 'https://rest.canopyapi.co/api/amazon/product' headers = { 'API-KEY': api_key # or 'Authorization': f'Bearer {api_key}' } response = requests.get(url, headers=headers) if response.status_code == 200: print(response.json()) else: print(f"Error: {response.status_code}") ``` -------------------------------- ### Import Hoppscotch Collection Source: https://docs.canopyapi.co/examples/hoppscotch Instructions for importing a pre-built collection of CanopyAPI queries into Hoppscotch. This simplifies testing and interacting with the API. ```Hoppscotch 1. Download our pre-built collection here. 2. Go to Hoppscotch 3. Import the collection file downloaded in step 1. 4. Add your API key to the `CANOPY-API-KEY` header in the header panel. ``` -------------------------------- ### Python GraphQL API Client Source: https://docs.canopyapi.co/contributing/how-to-contribute Illustrates how to interact with the CanopyAPI's GraphQL endpoint using Python. This snippet is helpful for Python-based applications consuming GraphQL data. ```Python import requests query = ''' { resource(id: "1") { field } } ''' response = requests.post('https://api.canopyapi.co/graphql', json={'query': query}) print(response.json()) ``` -------------------------------- ### Fetch GraphQL API (JavaScript) Source: https://docs.canopyapi.co/contributing/how-to-contribute Shows how to query the CanopyAPI's GraphQL endpoint using JavaScript's Fetch API. This is suitable for applications that prefer GraphQL for data fetching. ```JavaScript fetch('https://api.canopyapi.co/graphql', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ query: '{ resource(id: "1") { field } }' }), }) .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error('Error:', error)); ``` -------------------------------- ### Fetch REST API (JavaScript) Source: https://docs.canopyapi.co/contributing/how-to-contribute Demonstrates how to interact with the CanopyAPI's REST endpoints using JavaScript's Fetch API. This snippet is useful for frontend applications or Node.js environments that need to consume the API. ```JavaScript fetch('https://api.canopyapi.co/rest/v1/resource') .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error('Error:', error)); ``` -------------------------------- ### Fetch CanopyAPI Data (JavaScript) Source: https://docs.canopyapi.co/examples/hoppscotch Demonstrates how to fetch data from CanopyAPI using the Fetch API in JavaScript. This is useful for client-side applications or Node.js environments. ```JavaScript fetch('YOUR_API_ENDPOINT', { method: 'GET', headers: { 'CANOPY-API-KEY': 'YOUR_API_KEY' } }) .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error('Error:', error)); ``` -------------------------------- ### Fetch GraphQL API (JavaScript) Source: https://docs.canopyapi.co/index Shows how to query the Canopy API's GraphQL endpoint using JavaScript's fetch API. Requires an API key for authentication and a GraphQL query. ```JavaScript fetch('https://graphql.canopyapi.co/', { method: 'POST', headers: { 'Content-Type': 'application/json', 'API-KEY': 'YOUR_API_KEY' // or 'Authorization': 'Bearer YOUR_API_KEY' }, body: JSON.stringify({ query: '{ products(query: "example") { title price } }' }) }) .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error('Error:', error)); ``` -------------------------------- ### Fetch REST API (JavaScript) Source: https://docs.canopyapi.co/index Demonstrates how to make requests to the Canopy API's REST endpoint using JavaScript's fetch API. Requires an API key for authentication. ```JavaScript fetch('https://rest.canopyapi.co/api/amazon/product', { method: 'GET', headers: { 'API-KEY': 'YOUR_API_KEY' // or 'Authorization': 'Bearer YOUR_API_KEY' } }) .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error('Error:', error)); ``` -------------------------------- ### Query Amazon Data with Python Source: https://docs.canopyapi.co/examples/python Demonstrates how to query Amazon product data using Python's 'requests' library and the Canopy GraphQL API. Requires an API-KEY and specifies the GraphQL endpoint, query, headers, and variables for the request. ```Python import requests # Define the URL of the GraphQL endpointurl ='https://graphql.canopyapi.co/' # Define the GraphQL queryquery =""" query amazonProduct($asin: String!) { amazonProduct(input: {asin: $asin}) { title mainImageUrl rating price { display } } } """ headers ={'Content-Type':'application/json','API-KEY':'',} variables ={'asin':'B0B3JBVDYP',} # Define the request payloadpayload ={'query': query,'variables': variables } # Send the POST request to the GraphQL endpointresponse = requests.post(url, json=payload, headers=headers) if response.status_code ==200: data = response.json() print(data) else: print(f"Query failed to run with a {response.status_code} status code.") print(f"Response: {response.text}") ``` -------------------------------- ### Configure API Key in Apollo Server Schema Source: https://docs.canopyapi.co/examples/apollo-server This code snippet demonstrates how to replace a placeholder API key with your actual Canopy API key within the Apollo Server configuration file (`apollo/schema.ts`). This is crucial for authenticating requests to the Canopy API. ```typescript const remoteExecutor =buildHTTPExecutor({... headers:{ Authorization:"Bearer CANOPY-API-KEY", },}); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.