Rick and Morty API

repository·master·Indexed 21 days ago

https://github.com/afuh/rick-and-morty-api

A RESTful and GraphQL API providing access to canonical character, location, and episode data from the Rick and Morty TV show. The project includes a typed JavaScript client, data models for core entities, and a server implementation using Express and Apollo Server with MongoDB integration.

Tokens
1.5K
Snippets
6
Records
10
Agent score
77%

What's inside rick-morty-api

  1. Overview of The Rick and Morty API

    master
    The Rick and Morty API is a RESTful and GraphQL API that provides access to canonical data from the Rick and Morty television show. It includes data for hundreds of characters, locations, and episodes. Developers can interact with the API using standard HTTP requests or through various language-specific client libraries.
  2. Configure the Rick and Morty API server environment

    master

    The server uses environment variables for configuration. To run the server with specific settings, ensure the following variables are set in your environment or a .env file:

    • NODE_ENV: Determines the database connection string. If set to 'test', it uses a -test suffix on the local MongoDB URI. If set to 'production', it uses the DATABASE variable.
    • DATABASE: The connection string for the MongoDB instance when NODE_ENV is set to 'production'.
    • PORT: The port on which the Express server will listen. Defaults to 8080 if not provided.
    # Example .env file
    PORT=8080
    NODE_ENV=development
    DATABASE=mongodb://user:pass@remote-host:27017/rickmorty
  3. Access the REST and GraphQL API endpoints

    master

    Once the server is running, it exposes two primary interfaces:

    1. REST API: Available at the /api/ prefix (e.g., http://localhost:${PORT}/api/). Specific routes are defined in the routes module.
    2. GraphQL API: Available at the default Apollo Server path (usually /graphql). The server has introspection and playground enabled, allowing you to explore the schema and run queries via a web interface.

    Static Assets: Character avatars are served via the /api/character/avatar endpoint, which maps to the local images directory.

    REST      → http://localhost:${PORT}/api/
    GraphQL   → http://localhost:${PORT}${server.graphqlPath}/
  4. Explore community libraries and API collections

    master

    In addition to the official JavaScript client, several community-maintained libraries and collections are available for different environments:

    Language Clients

    API Collections

  5. Get the API base endpoints

    master

    A GET request to the root path (/) returns a JSON object containing the base URLs for the primary resources: characters, locations, and episodes. This is useful for discovering the entry points for the different resource collections.

    GET /
    
    {
      "characters": "<baseUrl>/character",
      "locations": "<baseUrl>/location",
      "episodes": "<baseUrl>/episode"
    }
  6. Access core data models for Characters, Locations, and Episodes

    master

    The models module serves as the central entry point for accessing the data schemas and models for the Rick and Morty API. It exports three primary models:

    • character: Represents character entities.
    • location: Represents location entities.
    • episode: Represents episode entities.

    You can import these models to interact with or validate data structures related to these specific entities.

    const models = require('./models');
    
    // Accessing specific models
    const Character = models.character;
    const Location = models.location;
    const Episode = models.episode;
  7. Reference: Server Environment Variables

    master

    The following environment variables control the server's behavior and connectivity.

    NODE_ENV      - Sets environment (test, production, etc.)
    DATABASE       - MongoDB connection string (used in production)
    PORT           - Port for the Express server (default: 8080)