Docs
Integrate Code Examples

Integrate Code Examples

How to use the custom endpoint with code

Prerequisites

Before you start, ensure you have the following:

  • programming language like JavaScript.
  • Basic understanding of HTTP requests and responses.

Examples

The following examples are written in Javascript.

Note: Before we start, note that you don't need to include the Content-Type field in the headers. The data type of the body is automatically detected and sent with the request when there is a body.

1. Get data

  • Use your Generated API Key and your new custom segment in your created API endpoint to start using the customized API responses.
fetch("https://mask.maskapi.dev/api/v1/users/{Your_new_custom_API_segment}", {
  method: "GET",
  headers: 
  { 
      "mask-api": "{Your_generated_api_key}" // Add your headers after mask-api if needed 
  },
})
  .then((res) => {
    if (res.ok) {
      return res.json();
    }
    // handle error
  })
  .then((data) => {
    // Do something with the list of data
  })
  .catch((error) => {
    // handle error
  });

2. Create New data

  const newData = {
  name: 'example_userName' // data
};
 
fetch("https://mask.maskapi.dev/api/v1/users/{Your_new_custom_API_segment}", {
  method: "POST",
  headers: 
  { 
      "mask-api": "{Your_generated_api_key}" // Add your headers after mask-api if needed 
  },
  // Send your data in the request body as JSON or formData it will automatically detected
  body: newData
}).then(res => {
  if (res.ok) {
      return res.json();
  }
  // handle error
}).then(data => {
  // do something with the new data
}).catch(error => {
  // handle error
})

3. Update data

  const updatedData = {
  name: 'example_updated_userName' // data
};
 
fetch("https://mask.maskapi.dev/api/v1/users/{Your_new_custom_API_segment}/1", {
  method: 'PUT', // or PATCH
  headers: 
  { 
      "mask-api": "{Your_generated_api_key}" // Add your headers after mask-api if needed 
  },
  body: updatedData
}).then(res => {
  if (res.ok) {
      return res.json();
  }
  // handle error
}).then(data => {
  // do something with the updated data
}).catch(error => {
  // handle error
})

4. Delete data

fetch("https://mask.maskapi.dev/api/v1/users/{Your_new_custom_API_segment}/1", { 
  method: 'DELETE',
  headers: 
  { 
      "mask-api": "{Your_generated_api_key}" // Add your headers after mask-api if needed 
  },
}).then(res => {
  if (res.ok) {
      return res.json();
  }
  // handle error
}).then(data => {
  // Do something with deleted data
}).catch(error => {
  // handle error
})

Note:

  • https://mask.maskapi.dev/api/v1/users/ is the base URl for all of your requests.
  • mask-api header is used to authenticate your API requests.

Queries and Params

Suppose we want to filter or add parameters to the URL. Fortunately, Mask API supports filtering by taking the sent search parameters and adding them to your original URL.

Here's an example for that:

const url = new URL('https://mask.maskapi.dev/api/v1/users/my-users');
url.searchParams.append('active', false); //https://mask.maskapi.dev/api/v1/users/my-users?active=false
// Mask API taking sends params and adding them to the original endpoint of your custom endpoint https://original.com/api/my-users?active=false
 
fetch(url, {
  method: 'GET',
  headers: {"mask-api": "{Your_generated_api_key}"},
}).then(res => {
  if (res.ok) {
      return res.json();
  }
  // handle error
}).then(users => {
  // returns only not active users
}).catch(error => {
  // handle error
})

Next Step