prism
Turn any OpenAPI2/3 and Postman Collection file into an API server with mocking, transformations and validations.
Top Related Projects
Newman is a command-line collection runner for Postman
Language-agnostic HTTP API Testing Tool
📘 OpenAPI/Swagger-generated API Reference Documentation
Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.
A flexible JSON/YAML linter for creating automated style guides, with baked in support for OpenAPI (v3.1, v3.0, and v2.0), Arazzo v1.0, as well as AsyncAPI v2.x.
Quick Overview
Prism is an open-source HTTP mock server and contract testing tool. It allows developers to simulate API behavior based on OpenAPI specifications, enabling faster development and testing of API-dependent applications without relying on actual backend services.
Pros
- Supports OpenAPI v2 and v3 specifications
- Provides dynamic response generation based on request parameters
- Offers a CLI tool for easy integration into development workflows
- Enables contract testing to ensure API compliance
Cons
- Limited support for complex validation scenarios
- May require additional configuration for certain edge cases
- Learning curve for advanced features and customizations
- Performance can be impacted when handling large OpenAPI specifications
Code Examples
- Starting Prism server with an OpenAPI specification:
prism mock api.yaml
- Using Prism with custom port and CORS enabled:
prism mock api.yaml --port 4010 --cors
- Validating requests against an OpenAPI specification:
prism proxy api.yaml http://api.example.com
Getting Started
- Install Prism globally using npm:
npm install -g @stoplight/prism-cli
-
Create an OpenAPI specification file (e.g.,
api.yaml
) describing your API. -
Start the mock server:
prism mock api.yaml
- Access the mock API endpoints using tools like cURL or Postman:
curl http://localhost:4010/your-endpoint
Competitor Comparisons
Newman is a command-line collection runner for Postman
Pros of Newman
- Extensive CLI options for customization and flexibility
- Seamless integration with Postman collections and environments
- Built-in reporting capabilities with various output formats
Cons of Newman
- Primarily focused on running Postman collections, less versatile for general API mocking
- Steeper learning curve for users not familiar with Postman ecosystem
- Limited built-in validation features compared to Prism
Code Comparison
Newman (running a collection):
newman run mycollection.json -e myenvironment.json --reporters cli,json
Prism (mocking an API):
prism mock openapi.yaml
Newman is a command-line tool for running Postman collections, while Prism is an API mocking and contract testing tool. Newman excels in executing and testing API requests defined in Postman collections, offering extensive reporting options. Prism, on the other hand, focuses on mocking APIs based on OpenAPI specifications, providing a simpler setup for API prototyping and testing.
Newman is ideal for teams already using Postman and requiring advanced test automation. Prism is better suited for developers looking for quick API mocking and validation against OpenAPI specs. The choice between the two depends on your specific workflow and API development needs.
Language-agnostic HTTP API Testing Tool
Pros of Dredd
- Supports multiple API description formats (API Blueprint, Swagger)
- Integrates well with continuous integration systems
- Offers detailed test reports and hooks for customization
Cons of Dredd
- Slower execution compared to Prism
- More complex setup and configuration
- Limited support for newer OpenAPI versions
Code Comparison
Dredd configuration example:
reporter: apiary
custom:
apiaryApiKey: yourApiaryApiKey
apiaryApiName: yourApiaryApiName
Prism usage example:
prism mock -d openapi.yaml
Key Differences
Prism focuses on API mocking and validation, while Dredd specializes in API testing and documentation. Prism offers faster performance and easier setup, making it ideal for local development and quick API prototyping. Dredd excels in comprehensive API testing scenarios, especially in CI/CD pipelines.
Both tools serve different primary purposes: Prism for mocking and Dredd for testing. The choice between them depends on specific project needs, with Prism being more suitable for rapid development cycles and Dredd for thorough API testing and documentation validation.
📘 OpenAPI/Swagger-generated API Reference Documentation
Pros of Redoc
- Focuses on API documentation rendering with a sleek, customizable UI
- Supports multiple themes and responsive design for better user experience
- Offers a standalone version that can be easily embedded in existing websites
Cons of Redoc
- Limited to documentation rendering, lacks API mocking capabilities
- May require additional setup for complex documentation scenarios
- Less frequent updates compared to Prism
Code Comparison
Redoc (HTML embedding):
<script src="https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js"></script>
<redoc spec-url="https://api.example.com/openapi.json"></redoc>
Prism (CLI usage):
prism mock api.yaml
Key Differences
- Prism is primarily an API mocking tool with additional features, while Redoc focuses on API documentation rendering
- Redoc offers a more polished UI for API documentation, whereas Prism provides a broader range of API development tools
- Prism has a more active development cycle and community support
- Redoc is better suited for projects that require visually appealing API documentation, while Prism is ideal for API testing and development workflows
Both tools serve different purposes in the API development ecosystem, with Redoc excelling in documentation presentation and Prism offering a comprehensive suite of API mocking and testing features.
Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.
Pros of Swagger UI
- Widely adopted and well-established in the API documentation ecosystem
- Offers a rich, interactive UI for exploring and testing API endpoints
- Supports multiple API specification formats (OpenAPI 2.0, 3.0, and Swagger)
Cons of Swagger UI
- Primarily focused on documentation and API exploration, not mocking or validation
- Can be resource-intensive for large API specifications
- Limited customization options for the UI without significant development effort
Code Comparison
Swagger UI (HTML setup):
<div id="swagger-ui"></div>
<script src="https://unpkg.com/swagger-ui-dist@3/swagger-ui-bundle.js"></script>
<script>
SwaggerUIBundle({
url: "https://petstore.swagger.io/v2/swagger.json",
dom_id: '#swagger-ui',
});
</script>
Prism (CLI usage):
prism mock openapi.yaml
Key Differences
- Prism focuses on API mocking and validation, while Swagger UI is primarily for documentation and exploration
- Swagger UI provides a visual interface out of the box, whereas Prism is a CLI tool
- Prism offers more advanced features for API testing and development workflows
A flexible JSON/YAML linter for creating automated style guides, with baked in support for OpenAPI (v3.1, v3.0, and v2.0), Arazzo v1.0, as well as AsyncAPI v2.x.
Pros of Spectral
- Focused on API linting and validation, offering more specialized rules and customization options
- Supports multiple API description formats (OpenAPI, AsyncAPI, JSON Schema)
- Easier to integrate into CI/CD pipelines for automated API governance
Cons of Spectral
- Limited to static analysis, doesn't provide API mocking capabilities
- Requires more setup and configuration for complex rulesets
- May have a steeper learning curve for creating custom rules
Code Comparison
Spectral (custom rule example):
module.exports = {
rules: {
'operation-description': {
given: '$.paths.*[get,post,put,patch,delete]',
then: {
field: 'description',
function: 'truthy'
}
}
}
};
Prism (usage example):
const { Prism } = require('@stoplight/prism-http');
const prism = new Prism({
specification: './openapi.yaml'
});
prism.server.listen(3000);
While Spectral focuses on linting and validation, Prism is primarily used for API mocking and testing. Spectral is better suited for ensuring API design quality, while Prism excels at simulating API behavior for development and testing purposes.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Prism Overview
Prism is a set of packages for API mocking and contract testing with OpenAPI v2 (formerly known as Swagger) and OpenAPI v3.x.
Prism provides:
- Mock Servers: Life-like mock servers from any API specification document.
- Validation Proxy: Contract Testing for API consumers and developers.
- Comprehensive API Specification Support: OpenAPI v3.1, OpenAPI v3.0, OpenAPI v2.0 (formerly Swagger) and Postman Collections.
Ways to Use Prism
Hosted Prism
Stoplight provides hosted mock servers for convenience so that API consumers can experiment with an API without the need for backend code.
Use one of these options for instant, hosted mock servers:
- Stoplight Platform: Collaborative API Design Platform for designing, developing and documenting APIs with hosted mocking powered by Prism.
- Stoplight Studio: Free visual OpenAPI designer that comes integrated with mocking powered by Prism.
Learn more in the hosted Prism documentation.
Self-hosted Prism
Prism is an open-source HTTP server run from the command-line. It provides mocking, request validation, and content negotiation. Use it standalone tool or in continuous integration.
Installation and Usage
This information refers to Open Source Prism 3.x, which is the current version most likely you will use. If you're looking for the 2.x version, look at the 2.x
branch
Install Self-hosted Prism
Prism requires
- NodeJS >= 18.20.1
- for NodeJS 18.x, >= 18.16 is required
npm install -g @stoplight/prism-cli
# OR
yarn global add @stoplight/prism-cli
For more installation options, see our installation documentation.
Mocking
Prism can help you create a fake "mock" based off an OpenAPI document, which helps people see how your API will work before you even have it built. Run it locally with the prism mock
command to run your API on a HTTP server you can interact with.
prism mock https://raw.githack.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore-expanded.yaml
Learn more about how the mock server works.
Validation Proxy
Prism can help you check for discrepencies between your API implementation and the OpenAPI document that describes, letting you funnel HTTP traffic through it with the prism proxy
command.
prism proxy examples/petstore.oas2.yaml https://petstore.swagger.io/v2
Learn more about how the validation proxy works.
ð Documentation and Community
â FAQs
Cannot access mock server when using Docker?
Prism uses localhost by default, which usually means 127.0.0.1. When using docker the mock server will
be unreachable outside of the container unless you run the mock command with -h 0.0.0.0
.
Why am I getting 404 errors when I include my basePath?
OpenAPI v2.0 had a concept called "basePath", which was essentially part of the HTTP path the stuff
after host name and protocol, and before query string. Unlike the paths in your paths
object, this
basePath was applied to every single URL, so Prism v2.x used to do the same. In OpenAPI v3.0 they
merged the basePath concept in with the server.url, and Prism v3 has done the same.
We treat OAS2 host + basePath
the same as OAS3 server.url
, so we do not require them to go in
the URL. If you have a base path of api/v1
and your path is defined as hello
, then a request to
http://localhost:4010/hello
would work, but http://localhost:4010/api/v1/hello
will fail. This
confuses some, but the other way was confusing to others. Check the default output of Prism CLI to
see what URLs you have available.
ð§ Roadmap
- Content Negotiation
- Security Validation
- Validation Proxy
- Recording/Learning Mode (create OpenAPI from HTTP traffic)
- Data Persistence (allow Prism act like a sandbox)
Submit your ideas for new functionality on the Stoplight Roadmap.
ð Help Others Utilize Prism
If you're using Prism for an interesting use case, contact us for a case study. We'll add it to a list here. Spread the goodness ð
ð Contributing
If you are interested in contributing to Prism itself, check out our contributing docs â and code of conduct â to get started.
ð Thanks
Prism is built on top of lots of excellent packages, and here are a few we'd like to say a special thanks to.
Check these projects out!
ð² Sponsor Prism by Planting a Tree
If you would like to thank us for creating Prism, we ask that you buy the world a tree.
Top Related Projects
Newman is a command-line collection runner for Postman
Language-agnostic HTTP API Testing Tool
📘 OpenAPI/Swagger-generated API Reference Documentation
Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.
A flexible JSON/YAML linter for creating automated style guides, with baked in support for OpenAPI (v3.1, v3.0, and v2.0), Arazzo v1.0, as well as AsyncAPI v2.x.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot