Build a Riddle via API

In this example you can see an easy demo of creating a riddle via the Riddle API.

Step 1

Create an API Access Token here: Access Token. Important: Delete this token after you finish testing here.

Step 2 Insert an access token, Title, Question and Answers and click on "Build Riddle". The Riddle will be created in your account and appears below.

Title

Question

Example Riddle JSON

{
    "type": "Poll",
    "publish": true,
    "build": {
        "title": "Favorite color poll",
        "blocks": [
            {
                "title": "What's your favorite color?",
                "type": "SingleChoice",
                "items": [
                    "green",
                    "red",
                    "blue",
                    "yellow",
                    "purple"
                ]
            }
        ],
        "result": {
            "title": "Thank you",
            "description": "Thank you for participating in our poll!"
        }
    }
}

Example Symfony PHP controller

This is an example of how you can create a Riddle in PHP with Symfony framework using the Riddle API.

<?php

// src/Controller/RiddleApiController.php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;

class ResponseDTO
{
    public string $message;
    public int $status;
    public $data;
}

class RiddleDTO
{
    public string $UUID;
    public string $title;
    public string $type;
}

class RiddleApiController extends AbstractController
{
    #[Route('/api/riddle-builder', name: 'app_riddle_builder', methods: ['POST'])]
    public function buildRiddle(Request $request, HttpClientInterface $httpClient, SerializerInterface $serializer): JsonResponse
    {
        // read the access token from the request json body post
        $data = json_decode($request->getContent(), true);
        $riddleAccessToken = $data['riddleAccessToken'] ?? null;

        if ($riddleAccessToken === null) {
            return new JsonResponse(['message' => 'Missing riddleAccessToken'], 400);
        }

        $riddleData = $data['riddleData'] ?? null;

        if ($riddleData === null) {
            return new JsonResponse(['message' => 'Missing riddleData'], 400);
        }

        $client = $httpClient->withOptions([
            'base_uri' => 'https://www.riddle.com',
            'headers' => [
                'X-RIDDLE-BEARER' => 'Bearer ' . $riddleAccessToken,
            ]
        ]);

        $response = $client->request(
            'POST',
            '/api/v3/riddle-builder',
            [
                'json' => $riddleData
            ]
        );

        $statusCode = $response->getStatusCode();

        $responseDTO = new ResponseDTO();
        $responseDTO->status = $statusCode;

        if ($statusCode !== 200) {
            $responseDTO->message = 'Error';

            return new JsonResponse($responseDTO, $statusCode);
        }

        $content = $response->toArray();

        $responseDTO->message = 'Success';

        $riddleDTO= new RiddleDTO();
        $riddleDTO->UUID = $content['data']['UUID'];
        $riddleDTO->title = $content['data']['title'];
        $riddleDTO->type = $content['data']['type'];

        $responseDTO->data = $riddleDTO;

        return new JsonResponse($responseDTO, $statusCode);
    }
}