Skip to content

Code Samples

Code Samples allow you to showcase API request examples in multiple programming languages. This feature helps API consumers understand how to integrate with your API using their preferred language.

You can customize:

  • Which languages to display (langs)
  • Available languages for selection (availableLangs)
  • How code is generated for each language (generator)

Custom Languages

For example, you can add Bru Markup Language to the list of languages to show and available languages to select from and a generator to convert the request object to Bru code.

markdown
---
aside: false
outline: false
title: vitepress-openapi
---

<script setup lang="ts">
import { useData } from 'vitepress'
import { useTheme, generateCodeSample } from 'vitepress-openapi'

const { isDark } = useData()

useTheme({
    codeSamples: {
        // List of languages to show in Code Samples section.
        langs: [
            'bruno',
            ...useTheme().getCodeSamplesLangs(),
        ],
        // List of available languages to select from.
        availableLanguages: [
            {
                lang: 'bruno',
                label: 'Bruno',
                highlighter: 'plaintext',
            },
            ...useTheme().getCodeSamplesAvailableLanguages(),
        ],
        defaultLang: 'bruno',
        generator: (lang, request) => {
            if (lang === 'bruno') {
                return generateBruRequest(request)
            }

            return generateCodeSample(lang, request)
        },
    },
})

function generateBruRequest(request) {
  const { url, method, headers, body, query } = request;

  const methodLower = method.toLowerCase();

  const queryString = query && Object.keys(query).length
    ? `${url}?${new URLSearchParams(query).toString()}`
    : url;

  const headersSection = headers && Object.keys(headers).length
    ? `headers {\n${Object.entries(headers)
        .map(([key, value]) => `  ${key}: ${value}`)
        .join('\n')}\n}`
    : '';

  const bodySection = body
    ? `body {\n  ${JSON.stringify(body, null, 2).replace(/\n/g, '\n  ')}\n}`
    : '';

  const bruRequest = `${methodLower} {
  url: ${queryString}
}

${headersSection}

${bodySection}
`;

  return bruRequest
        .trim()
        .replace(/\n{2,}/g, '\n\n') // Remove extra newlines
}
</script>

<OASpec :isDark="isDark" />
Example
v1.0.0

Argentine Rock Legends

The Argentine Rock Legends is an example OpenAPI specification to test OpenAPI tools and libraries. Get all the data for all artists.

Inspired by Scalar Galaxy

Resources

  • https://github.com/enzonotario/vitepress-openapi
  • https://github.com/OAI/OpenAPI-Specification

Markdown Support

All descriptions can contain tons of text Markdown. If GitHub supports the syntax, chances are we’re supporting it, too. You can even create internal links to reference endpoints.

Examples

Blockquotes

I love Argentine Rock. <3

Tables

Feature Availability
Markdown Support

Accordion

<details>
  <summary>Using Details Tags</summary>
  <p>HTML Example</p>
</details>

Images

Yes, there’s support for images, too!

Placeholder image

Contact

Servers

https://stoplight.io/mocks/enzonotario/argentine-rock/122547792Mock Server

Artists


Get all artists

GET
/api/v1/artists

Get a list of all legendary Argentine Rock artists and explore their contributions to the music scene.

Parameters

Query Parameters

limit

The number of items to return

Typeinteger
offset

The number of items to skip before starting to collect the result set

Typeinteger

Responses

OK
JSON
{
"data": [
{
"id": 1,
"name": "Charly García",
"description": "One of the most influential rock musicians in Argentine history.",
"image": "https://cdn.rock-legends.com/photos/charly.jpg",
"band": "Sui Generis"
}
],
"meta": {
"limit": 10,
"offset": 0,
"total": 100,
"next": "/artists?limit=10&offset=10"
}
}

Samples

Bruno
get {
  url: https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists
}

headers {
  Content-Type: application/json
}
cURL
curl -X GET \
'https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists' \
 -H "Content-Type: application/json"
JavaScript
fetch('https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists', {headers:{'Content-Type':'application/json'}})
  .then(response => response.json())
  .then(data => console.log(data));
PHP
<?php
$url = 'https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists';
$method = 'GET';
$headers = [
    'Content-Type' => 'application/json',
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);
curl_close($ch);

echo $response;
?>
Python
import requests

url = 'https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists'

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

response = requests.get(url, headers=headers)
print(response.json())

Add a new artist

POST
/api/v1/artists

Add a new legendary Argentine Rock artist. Make sure they truly deserve the title!

Authorizations

Request Body

JSON
{
"id": 1,
"name": "Charly García",
"description": "One of the most influential rock musicians in Argentine history.",
"image": "https://cdn.rock-legends.com/photos/charly.jpg",
"band": "Sui Generis"
}

Responses

Created
JSON
{
"id": 1,
"name": "Charly García",
"description": "One of the most influential rock musicians in Argentine history.",
"image": "https://cdn.rock-legends.com/photos/charly.jpg",
"band": "Sui Generis"
}

Samples

Bruno
post {
  url: https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists
}

headers {
  Content-Type: application/json
}

body {
  {
    "id": 1,
    "name": "Charly García",
    "description": "One of the most influential rock musicians in Argentine history.",
    "image": "https://cdn.rock-legends.com/photos/charly.jpg",
    "band": "Sui Generis"
  }
}
cURL
curl -X POST \
'https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists' \
 -H "Content-Type: application/json" \
 --data '{
  "id": 1,
  "name": "Charly García",
  "description": "One of the most influential rock musicians in Argentine history.",
  "image": "https://cdn.rock-legends.com/photos/charly.jpg",
  "band": "Sui Generis"
}'
JavaScript
fetch('https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists', {method:'POST',headers:{'Content-Type':'application/json'},body:'{"id":1,"name":"Charly García","description":"One of the most influential rock musicians in Argentine history.","image":"https://cdn.rock-legends.com/photos/charly.jpg","band":"Sui Generis"}'})
  .then(response => response.json())
  .then(data => console.log(data));
PHP
<?php
$url = 'https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists';
$method = 'POST';
$headers = [
    'Content-Type' => 'application/json',
];
$body = json_encode({"id":1,"name":"Charly García","description":"One of the most influential rock musicians in Argentine history.","image":"https://cdn.rock-legends.com/photos/charly.jpg","band":"Sui Generis"});

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

$response = curl_exec($ch);
curl_close($ch);

echo $response;
?>
Python
import requests

url = 'https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists'

headers = {
    'Content-Type': 'application/json'
}
data = {
    'id': 1,
    'name': 'Charly García',
    'description': 'One of the most influential rock musicians in Argentine history.',
    'image': 'https://cdn.rock-legends.com/photos/charly.jpg',
    'band': 'Sui Generis'
}

response = requests.post(url, headers=headers, json=data)
print(response.json())

Get an artist

GET
/api/v1/artists/{artistId}

Learn more about a specific Argentine Rock artist and their legacy.

Parameters

Path Parameters

artistId*
Typeinteger
Required
Example1

Responses

Artist Found
JSON
{
"id": 1,
"name": "Charly García",
"description": "One of the most influential rock musicians in Argentine history.",
"image": "https://cdn.rock-legends.com/photos/charly.jpg",
"band": "Sui Generis"
}

Samples

Bruno
get {
  url: https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists/1
}

headers {
  Content-Type: application/json
}
cURL
curl -X GET \
'https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists/1' \
 -H "Content-Type: application/json"
JavaScript
fetch('https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists/1', {headers:{'Content-Type':'application/json'}})
  .then(response => response.json())
  .then(data => console.log(data));
PHP
<?php
$url = 'https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists/1';
$method = 'GET';
$headers = [
    'Content-Type' => 'application/json',
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);
curl_close($ch);

echo $response;
?>
Python
import requests

url = 'https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists/1'

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

response = requests.get(url, headers=headers)
print(response.json())

Update an artist

PUT
/api/v1/artists/{artistId}

Update the information of a legendary Argentine Rock artist. Make sure to provide accurate data.

Authorizations

Parameters

Path Parameters

artistId*
Typeinteger
Required
Example1

Request Body

JSON
{
"id": 1,
"name": "Charly García",
"description": "One of the most influential rock musicians in Argentine history.",
"image": "https://cdn.rock-legends.com/photos/charly.jpg",
"band": "Sui Generis"
}

Responses

OK
JSON
{
"id": 1,
"name": "Charly García",
"description": "One of the most influential rock musicians in Argentine history.",
"image": "https://cdn.rock-legends.com/photos/charly.jpg",
"band": "Sui Generis"
}

Samples

Bruno
put {
  url: https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists/1
}

headers {
  Content-Type: application/json
}

body {
  {
    "id": 1,
    "name": "Charly García",
    "description": "One of the most influential rock musicians in Argentine history.",
    "image": "https://cdn.rock-legends.com/photos/charly.jpg",
    "band": "Sui Generis"
  }
}
cURL
curl -X PUT \
'https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists/1' \
 -H "Content-Type: application/json" \
 --data '{
  "id": 1,
  "name": "Charly García",
  "description": "One of the most influential rock musicians in Argentine history.",
  "image": "https://cdn.rock-legends.com/photos/charly.jpg",
  "band": "Sui Generis"
}'
JavaScript
fetch('https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists/1', {method:'PUT',headers:{'Content-Type':'application/json'},body:'{"id":1,"name":"Charly García","description":"One of the most influential rock musicians in Argentine history.","image":"https://cdn.rock-legends.com/photos/charly.jpg","band":"Sui Generis"}'})
  .then(response => response.json())
  .then(data => console.log(data));
PHP
<?php
$url = 'https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists/1';
$method = 'PUT';
$headers = [
    'Content-Type' => 'application/json',
];
$body = json_encode({"id":1,"name":"Charly García","description":"One of the most influential rock musicians in Argentine history.","image":"https://cdn.rock-legends.com/photos/charly.jpg","band":"Sui Generis"});

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

$response = curl_exec($ch);
curl_close($ch);

echo $response;
?>
Python
import requests

url = 'https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists/1'

headers = {
    'Content-Type': 'application/json'
}
data = {
    'id': 1,
    'name': 'Charly García',
    'description': 'One of the most influential rock musicians in Argentine history.',
    'image': 'https://cdn.rock-legends.com/photos/charly.jpg',
    'band': 'Sui Generis'
}

response = requests.put(url, headers=headers, json=data)
print(response.json())

Deprecated

Delete an artist

DELETE
/api/v1/artists/{artistId}

This endpoint was used to delete artists. Unfortunately, that caused a lot of controversy. So, this endpoint is now deprecated and should not be used anymore.

Authorizations

Parameters

Path Parameters

artistId*
Typeinteger
Required
Example1

Responses

Bad Request
JSON
{
"type": "https://example.com/errors/generic-error",
"title": "Something went wrong here.",
"status": 403,
"detail": "Unfortunately, we can’t provide further information."
}

Samples

Bruno
delete {
  url: https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists/1
}

headers {
  Content-Type: application/json
}
cURL
curl -X DELETE \
'https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists/1' \
 -H "Content-Type: application/json"
JavaScript
fetch('https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists/1', {method:'DELETE',headers:{'Content-Type':'application/json'}})
  .then(response => response.json())
  .then(data => console.log(data));
PHP
<?php
$url = 'https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists/1';
$method = 'DELETE';
$headers = [
    'Content-Type' => 'application/json',
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);
curl_close($ch);

echo $response;
?>
Python
import requests

url = 'https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists/1'

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

response = requests.delete(url, headers=headers)
print(response.json())

Get all albums

GET
/api/v1/artists/{artistId}/albums

Get a list of all albums from a legendary Argentine Rock artist.

Parameters

Path Parameters

artistId*
Typeinteger
Required
Example1

Query Parameters

limit

The number of items to return

Typeinteger
offset

The number of items to skip before starting to collect the result set

Typeinteger

Responses

OK
JSON
{
"data": [
{
"id": 1,
"name": "La Máquina de Hacer Pájaros",
"year": 1976,
"image": "https://cdn.rock-legends.com/photos/la-maquina.jpg"
}
],
"meta": {
"limit": 10,
"offset": 0,
"total": 100,
"next": "/artists?limit=10&offset=10"
}
}

Samples

Bruno
get {
  url: https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists/1/albums
}

headers {
  Content-Type: application/json
}
cURL
curl -X GET \
'https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists/1/albums' \
 -H "Content-Type: application/json"
JavaScript
fetch('https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists/1/albums', {headers:{'Content-Type':'application/json'}})
  .then(response => response.json())
  .then(data => console.log(data));
PHP
<?php
$url = 'https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists/1/albums';
$method = 'GET';
$headers = [
    'Content-Type' => 'application/json',
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);
curl_close($ch);

echo $response;
?>
Python
import requests

url = 'https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists/1/albums'

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

response = requests.get(url, headers=headers)
print(response.json())

Add a new album

POST
/api/v1/artists/{artistId}/albums

Add a new album to a legendary Argentine Rock artist. Make sure it’s a masterpiece!

Authorizations

Parameters

Path Parameters

artistId*
Typeinteger
Required
Example1

Request Body

JSON
{
"id": 1,
"name": "La Máquina de Hacer Pájaros",
"year": 1976,
"image": "https://cdn.rock-legends.com/photos/la-maquina.jpg"
}

Responses

Created
JSON
{
"id": 1,
"name": "La Máquina de Hacer Pájaros",
"year": 1976,
"image": "https://cdn.rock-legends.com/photos/la-maquina.jpg"
}

Samples

Bruno
post {
  url: https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists/1/albums
}

headers {
  Content-Type: application/json
}

body {
  {
    "id": 1,
    "name": "La Máquina de Hacer Pájaros",
    "year": 1976,
    "image": "https://cdn.rock-legends.com/photos/la-maquina.jpg"
  }
}
cURL
curl -X POST \
'https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists/1/albums' \
 -H "Content-Type: application/json" \
 --data '{
  "id": 1,
  "name": "La Máquina de Hacer Pájaros",
  "year": 1976,
  "image": "https://cdn.rock-legends.com/photos/la-maquina.jpg"
}'
JavaScript
fetch('https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists/1/albums', {method:'POST',headers:{'Content-Type':'application/json'},body:'{"id":1,"name":"La Máquina de Hacer Pájaros","year":1976,"image":"https://cdn.rock-legends.com/photos/la-maquina.jpg"}'})
  .then(response => response.json())
  .then(data => console.log(data));
PHP
<?php
$url = 'https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists/1/albums';
$method = 'POST';
$headers = [
    'Content-Type' => 'application/json',
];
$body = json_encode({"id":1,"name":"La Máquina de Hacer Pájaros","year":1976,"image":"https://cdn.rock-legends.com/photos/la-maquina.jpg"});

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

$response = curl_exec($ch);
curl_close($ch);

echo $response;
?>
Python
import requests

url = 'https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/artists/1/albums'

headers = {
    'Content-Type': 'application/json'
}
data = {
    'id': 1,
    'name': 'La Máquina de Hacer Pájaros',
    'year': 1976,
    'image': 'https://cdn.rock-legends.com/photos/la-maquina.jpg'
}

response = requests.post(url, headers=headers, json=data)
print(response.json())

Authentication

Some endpoints are public, but some require authentication. We provide all the required endpoints to create an account and authorize yourself.


Create a user

POST
/api/v1/user/signup

Create a user account to access exclusive content about Argentine Rock legends.

Request Body

JSON
{
"name": "Maria",
"email": "carlos@rock-legends.com",
"password": "rock-n-roll"
}

Responses

Created
JSON
{
"id": 1,
"name": "Carlos",
"email": "carlos@rock-legends.com"
}

Samples

Bruno
post {
  url: https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/user/signup
}

headers {
  Content-Type: application/json
}

body {
  {
    "name": "Maria",
    "email": "carlos@rock-legends.com",
    "password": "rock-n-roll"
  }
}
cURL
curl -X POST \
'https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/user/signup' \
 -H "Content-Type: application/json" \
 --data '{
  "name": "Maria",
  "email": "carlos@rock-legends.com",
  "password": "rock-n-roll"
}'
JavaScript
fetch('https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/user/signup', {method:'POST',headers:{'Content-Type':'application/json'},body:'{"name":"Maria","email":"carlos@rock-legends.com","password":"rock-n-roll"}'})
  .then(response => response.json())
  .then(data => console.log(data));
PHP
<?php
$url = 'https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/user/signup';
$method = 'POST';
$headers = [
    'Content-Type' => 'application/json',
];
$body = json_encode({"name":"Maria","email":"carlos@rock-legends.com","password":"rock-n-roll"});

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

$response = curl_exec($ch);
curl_close($ch);

echo $response;
?>
Python
import requests

url = 'https://stoplight.io/mocks/enzonotario/argentine-rock/122547792/api/v1/user/signup'

headers = {
    'Content-Type': 'application/json'
}
data = {
    'name': 'Maria',
    'email': 'carlos@rock-legends.com',
    'password': 'rock-n-roll'
}

response = requests.post(url, headers=headers, json=data)
print(response.json())

Powered by VitePress OpenAPI