Skip to main content
POST
/
auth
/
tokens
Create Token (Login)
curl --request POST \
  --url https://api.petstoreapi.com/v1/auth/tokens \
  --header 'Content-Type: application/json' \
  --data '
{
  "username": "kylekinh",
  "password": "SecurePass123!"
}
'
import requests

url = "https://api.petstoreapi.com/v1/auth/tokens"

payload = {
"username": "kylekinh",
"password": "SecurePass123!"
}
headers = {"Content-Type": "application/json"}

response = requests.post(url, json=payload, headers=headers)

print(response.text)
const options = {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({username: 'kylekinh', password: 'SecurePass123!'})
};

fetch('https://api.petstoreapi.com/v1/auth/tokens', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));
<?php

$curl = curl_init();

curl_setopt_array($curl, [
CURLOPT_URL => "https://api.petstoreapi.com/v1/auth/tokens",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'username' => 'kylekinh',
'password' => 'SecurePass123!'
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json"
],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
package main

import (
"fmt"
"strings"
"net/http"
"io"
)

func main() {

url := "https://api.petstoreapi.com/v1/auth/tokens"

payload := strings.NewReader("{\n \"username\": \"kylekinh\",\n \"password\": \"SecurePass123!\"\n}")

req, _ := http.NewRequest("POST", url, payload)

req.Header.Add("Content-Type", "application/json")

res, _ := http.DefaultClient.Do(req)

defer res.Body.Close()
body, _ := io.ReadAll(res.Body)

fmt.Println(string(body))

}
HttpResponse<String> response = Unirest.post("https://api.petstoreapi.com/v1/auth/tokens")
.header("Content-Type", "application/json")
.body("{\n \"username\": \"kylekinh\",\n \"password\": \"SecurePass123!\"\n}")
.asString();
require 'uri'
require 'net/http'

url = URI("https://api.petstoreapi.com/v1/auth/tokens")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n \"username\": \"kylekinh\",\n \"password\": \"SecurePass123!\"\n}"

response = http.request(request)
puts response.read_body
{
  "token": "<string>",
  "expiresAt": "2023-11-07T05:31:56Z"
}
{
"type": "https://petstoreapi.com/errors/unauthorized",
"title": "Unauthorized",
"status": 401,
"detail": "Authentication is required to access this resource. Please provide a valid OAuth 2.0 token."
}
{
"type": "https://petstoreapi.com/errors/validation-error",
"title": "Validation Error",
"status": 422,
"detail": "The request body contains validation errors.",
"instance": "/pets",
"errors": [
{
"field": "name",
"message": "Pet name is required",
"code": "required_field"
},
{
"field": "status",
"message": "Invalid status value",
"code": "invalid_format"
}
]
}
{
"type": "https://petstoreapi.com/errors/rate-limit-exceeded",
"title": "Too Many Requests",
"status": 429,
"detail": "Rate limit exceeded. Please wait 60 seconds before making another request."
}

Body

application/json
username
string
required

Login username

Example:

"johndoe"

password
string
required
write-only

User password (never returned in responses)

Example:

"securePassword123"

Response

Login successful

token
string

Authentication token

expiresAt
string<date-time>

Token expiration time