REST API Mocking on Hugging Face using Prism

Vishal Mysore
3 min readApr 13, 2024

--

Overview

When you’re developing applications that rely on REST APIs, mocking these APIs can be a vital part of your workflow, especially when the actual endpoints are not yet available. Prism is a useful tool for this purpose. It allows you to create mock servers that simulate the behavior of your REST APIs based on OpenAPI specifications.

Using Prism, you can validate API responses, test your application under various scenarios, and ensure that your app handles API data correctly, all before your actual API goes live. This makes Prism an excellent choice for developers looking to streamline frontend and backend development processes by simulating and testing API interactions efficiently.

Prism

Prism is a powerful open-source tool designed for API mocking and server simulation, particularly useful in the development stages of software that relies on web APIs. Developed by Stoplight, Prism allows developers to mimic the behavior of a live API by using OpenAPI (formerly Swagger) specifications. This capability enables you to validate API requests and responses, even without the backend being fully implemented.

With Prism, you can run mock servers that return realistic responses, test for error handling, and generate static examples or dynamic responses based on the API’s description. This makes it a crucial tool for both frontend and backend developers, ensuring that they can continue working in parallel without waiting for API endpoints to be built. Prism’s ability to simulate different scenarios and validate API behavior against the contract defined in the OpenAPI spec makes it indispensable for robust API testing and development workflows.

Steps

Create a new space on Huggingface from here

Upload a Dockerfile or create new one from files section

FROM node:14

# Set the working directory in the container
WORKDIR /app

# Copy the application files to the container
COPY . .

# Install application dependencies and Prism CLI
RUN npm install
RUN npm install -g @stoplight/prism-cli

# Make sure your OpenAPI spec file is in the container
#COPY ./path/to/your/openapi.yaml /app/openapi.yaml

# Expose the port Prism listens on
EXPOSE 7860

# Start Prism and mock the API when the container launches
CMD ["prism", "mock", "-h", "0.0.0.0", "-p","7860 ","kibernetes.json"]

as you can see its going to start Prism and mock all the endpoints discibed in kibernetes.json. Your Json file should be in Openapi format

openapi: 3.0.0
info:
title: Kubernetes Pods Listing API
version: 1.0.0
description: A mock Kubernetes API to list Pods in a specified namespace.
servers:
- url: http://localhost:4010
paths:
/api/v1/namespaces/{namespace}/pods:
get:
summary: List all pods in a namespace
operationId: listNamespacedPod
tags:
- Pods
parameters:
- name: namespace
in: path
required: true
schema:
type: string
example: enterpriseCluster
description: The namespace to list Pods in
responses:
'200':
description: A list of Pods
content:
application/json:
schema:
$ref: '#/components/schemas/PodList'
components:
schemas:
PodList:
type: object
properties:
kind:
type: string
example: PodList
apiVersion:
type: string
example: v1
items:
type: array
items:
$ref: '#/components/schemas/Pod'
Pod:
type: object
properties:
metadata:
$ref: '#/components/schemas/Metadata'
spec:
$ref: '#/components/schemas/PodSpec'
status:
$ref: '#/components/schemas/PodStatus'
Metadata:
type: object
properties:
name:
type: string
example: example-pod-1
namespace:
type: string
example: default
labels:
type: object
additionalProperties:
type: string
PodSpec:
type: object
properties:
containers:
type: array
items:
$ref: '#/components/schemas/Container'
Container:
type: object
properties:
name:
type: string
example: web-container
image:
type: string
example: nginx:latest
ports:
type: array
items:
$ref: '#/components/schemas/ContainerPort'
ContainerPort:
type: object
properties:
containerPort:
type: integer
example: 80
PodStatus:
type: object
properties:
phase:
type: string
example: Running

You should be able to access your rest end points in similar url

https://vishalmysore-mock.hf.space/api/v1/namespaces/dolorem/pods

Look at the working example here

--

--