Getting started overview
This guide outlines the process for new users to begin using Mocky for API mocking. It covers account creation, the generation of a mock API endpoint, and successfully making a first request to that endpoint. Mocky is designed for scenarios requiring quick, temporary API simulations, such as frontend development, unit testing, or demonstrating concepts without relying on a fully implemented backend Mocky documentation portal. While Mocky provides a straightforward approach to creating static mock responses, developers requiring more complex behaviors like dynamic data, advanced request matching, or stateful interactions may consider alternative tools such as OpenAPI Specification-based mock servers or dedicated API virtualization platforms Swagger tools documentation.
The core workflow involves:
- Navigating to the Mocky website.
- (Optional, but recommended) Registering for an account to manage mocks.
- Defining the desired HTTP response, including status code, headers, and body.
- Generating a unique URL for the mock API endpoint.
- Making an HTTP request to the generated URL to verify the mock.
The following table provides a quick reference for the steps involved:
| Step | What to Do | Where |
|---|---|---|
| 1. Access Mocky | Open the Mocky homepage | Mocky main website |
| 2. Create Account (Optional) | Register for a free account | Mocky signup page |
| 3. Define Mock | Specify HTTP response body, status, headers | Mocky web interface |
| 4. Generate URL | Click "Generate my HTTP Response" | Mocky web interface |
| 5. Test Endpoint | Make an HTTP request to the generated URL | Client-side code, cURL, Postman, browser |
Create an account and get keys
While Mocky allows for generating temporary mocks without an account, creating a free account is recommended for managing and reusing mocks, especially since anonymous mocks expire after 24 hours Mocky expiration policies. A free account supports up to 50 active mocks Mocky premium features.
Account Creation Steps:
- Navigate to the Mocky homepage.
- Locate the "Sign Up" or "Login" option, typically in the header.
- Choose to sign up using an email address and password, or use a supported third-party identity provider like Google or GitHub.
- Complete the registration process, which may involve email verification.
- Once logged in, you will be redirected to your dashboard or the mock creation interface. Mocky does not issue traditional API keys for accessing its service; instead, each mock you create has a unique, publicly accessible URL that serves as its identifier for consumption.
The primary 'key' in Mocky's context is the unique URL generated for each mock endpoint. This URL is publicly accessible and does not require authentication for clients to consume the mock response. Authentication is only required for the user managing their own mocks on the Mocky platform.
Your first request
Making your first request involves two main parts: defining the mock response and then calling the generated URL. This process does not require any programming knowledge to set up the mock itself.
Defining a Mock Response:
- Access the Mock Creation Interface: After logging in (or directly on the homepage if not logged in), you will see fields to define your HTTP response.
- Set HTTP Status Code: In the "Status" field, enter the desired HTTP status code. For example,
200for a successful response,404for Not Found, or500for a server error. - Define Response Headers: Use the "Headers" section to add custom HTTP headers. Headers are key-value pairs. For instance, to return JSON content, you would add a header with
Content-Typeas the key andapplication/jsonas the value. Multiple headers can be added. - Add Response Body: In the "Body" textarea, enter the content you want the mock API to return. This can be JSON, XML, plain text, or any other format. For example, a simple JSON body might look like:
{ "message": "Hello from Mocky!", "status": "success" } - Generate the Mock URL: Click the "Generate my HTTP Response" button. Mocky will then provide you with a unique URL (e.g.,
https://run.mocky.io/v3/[YOUR_UNIQUE_ID]). This is your mock API endpoint. Note this URL, as it is what your client applications will call.
Making the First Call:
Once you have your mock URL, you can test it using various tools:
Using a Web Browser:
Simply paste the generated Mocky URL into your web browser's address bar and press Enter. The browser will display the plain text or JSON response body you defined, depending on the Content-Type header. This is the simplest method for a quick verification.
Using cURL (Command Line):
cURL is a command-line tool for making HTTP requests and is available on most operating systems cURL official documentation. Open your terminal or command prompt and execute a command similar to this, replacing [YOUR_UNIQUE_ID] with your actual mock ID:
curl -X GET "https://run.mocky.io/v3/[YOUR_UNIQUE_ID]"
The terminal will display the full HTTP response, including headers and the body.
Using JavaScript (Fetch API):
If you're testing from a web application, you can use the browser's native Fetch API. Include this code in your HTML or JavaScript file:
fetch('https://run.mocky.io/v3/[YOUR_UNIQUE_ID]')
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json(); // or .text() if not JSON
})
.then(data => console.log('Mock response data:', data))
.catch(error => console.error('Error fetching mock:', error));
Replace [YOUR_UNIQUE_ID] with your mock's ID. This snippet will log the JSON response data to the browser's console.
Common next steps
After successfully creating and calling your first Mocky endpoint, consider these common next steps to further integrate and manage your mocks:
- Edit Existing Mocks: Log in to your Mocky account to view and edit your previously created mocks. You can modify the status code, headers, and body of any existing mock through the dashboard.
- Set Expiration: For temporary testing, you can specify an expiration time for your mocks (e.g., 1 hour, 1 day). For longer-term needs, consider premium features for extended expiry Mocky premium options.
- Utilize HTTPS: All generated Mocky URLs automatically use HTTPS, ensuring encrypted communication for your mock API calls, which is a standard security practice for web APIs Mozilla HTTPS explanation.
- Integrate with Development Workflow: Incorporate Mocky endpoints into your frontend development, mobile app development, or unit testing processes. Replace calls to your actual backend API with calls to the Mocky endpoint during initial development phases.
- Explore Premium Features: If your project requires more than 50 active mocks, custom domains for your mock endpoints, or longer expiration times, review the Mocky premium options.
- Share Mocks: The generated URL is shareable, allowing team members or collaborators to consume the same mock API endpoint without needing access to your Mocky account.
Troubleshooting the first call
If your first call to a Mocky endpoint does not return the expected response, consider these common troubleshooting steps:
- Verify the URL: Double-check that the URL you are calling exactly matches the one provided by Mocky after generation. Any typo can lead to a
404 Not Foundor an incorrect response. - Check Mock Status: Log in to your Mocky account and ensure that the mock you created is still active and has not expired. Anonymous mocks expire after 24 hours Mocky mock expiration details.
- Inspect HTTP Status Code: Confirm that the mock is configured to return the correct HTTP status code (e.g.,
200 OK). If you intended a200but received a404, it might indicate an issue with the URL or mock availability. - Review Headers: Ensure that the
Content-Typeheader is correctly set in your mock definition (e.g.,application/jsonfor JSON responses). If this header is missing or incorrect, your client might misinterpret the response body. - Examine Response Body: Verify that the content of the response body in your mock definition matches what you expect. Small errors or malformed JSON can cause parsing issues on the client side.
- Browser Caching: If testing in a browser, clear your browser's cache or use an incognito/private browsing window, as browsers can sometimes cache HTTP responses.
- Network Issues: Confirm that your client application or network environment has internet connectivity and is not blocked by a firewall from accessing
run.mocky.io. - Client-Side Code Errors: If using a code client (like Fetch API or libraries), check your client-side code for any syntax errors or incorrect handling of promises/callbacks. Use your browser's developer tools (console and network tabs) for detailed insights into the request and response.
- Mocky Service Status: In rare cases, Mocky's service might experience an outage. While there isn't a public status page readily available through their documentation, general internet connectivity issues can sometimes affect external services.