axe-linter Server REST API Example Usage

Link to axe-linter Server REST API Example Usage copied to clipboard

This article shows example code for accessing the axe-linter Server with curl and Python.

curl

The following example shows how to use the curl command line tool to POST requests to the server:

curl --request POST \
  --url http://localhost:3000/lint-source \
  --header 'Content-Type: application/json' \
  --data '{
        "source":"import React from \"react\";const testing = (props) => {    return (        <div>            <button tabIndex={5}>{props.name}</button>            <button tabIndex={5}>{props.name}</button>            <button tabIndex=\"5\">{props.name}</button>        </div>    );}\nexport default testing;",
        "filename": "string.js"
}'

To use the above sample with axe-linter SaaS, you need to add the following command-option:

--header 'Authorization: <YOUR API KEY>' \

You also need to change the --url argument to:

--url https://axe-linter.deque.com/lint-source

The sample updated for axe-linter SaaS is as follows:

curl --request POST \
  --url https://axe-linter.deque.com/lint-source \
  --header 'Content-Type: application/json' \
  --header 'Authorization: <YOUR API KEY>' \
  --data '{
        "source":"import React from \"react\";const testing = (props) => {    return (        <div>            <button tabIndex={5}>{props.name}</button>            <button tabIndex={5}>{props.name}</button>            <button tabIndex=\"5\">{props.name}</button>        </div>    );}\nexport default testing;",
        "filename": "string.js"
}'

See Obtaining an axe-linter SaaS API Key for more information about obtaining an API key for use with axe-linter SaaS.

Python

The following example shows how to access the axe-linter Server using Python 3:

import requests

baseUrl = "http://localhost:3000/lint-source"

headers = {
      "Content-Type": "application/json"
      }

data = {
  "source": "# Heading\n\n### Another Heading\n",
  "filename": "file.md"
     }

response = requests.post(baseUrl, json=data, headers=headers)

print(response.text)

To change the previous Python sample to work with axe-linter SaaS, you need to obtain an API key as detailed in Obtaining an axe-linter SaaS API Key and change baseUrl as follows:

baseUrl = "https://axe-linter.deque.com/lint-source"

You also need to add your API key to the request header by changing headers to the following:

headers = {
      "Content-Type": "application/json",
      "Authorization": "YOUR API KEY"
      }

Replace YOUR API KEY with the key you obtained earlier.