{"id":1960612,"date":"2024-07-01T19:15:22","date_gmt":"2024-07-01T23:15:22","guid":{"rendered":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/?p=1960612"},"modified":"2024-10-24T11:55:23","modified_gmt":"2024-10-24T15:55:23","slug":"rest-api-authentication","status":"publish","type":"post","link":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/rest-api-authentication\/","title":{"rendered":"REST API Authentication"},"content":{"rendered":"\n<p>Some REST API requests require authentication to return information for a request &#8211; for example the endpoint:<\/p>\n\n\n\n<p class=\"has-text-align-center\"><code>GET <\/code><a href=\"https:\/\/demo.theeventscalendar.com\/wp-json\/tribe\/tickets\/v1\/attendees \"><code>https:\/\/demo.theeventscalendar.com\/wp-json\/tribe\/tickets\/v1\/attendees<\/code> <\/a><\/p>\n\n\n\n<p>is a valid <a href=\"https:\/\/docs.theeventscalendar.com\/rest-endpoints\/?urls.primaryName=Tickets\" target=\"_blank\" rel=\"noreferrer noopener\">endpoint<\/a> to return all attendees for the given site, but without proper authentication, a site that has attendees will still return:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"rest_url\": \"https:\/\/demo.theeventscalendar.com\/wp-json\/tribe\/tickets\/v1\/attendees\/\",\n  \"total\": 0,\n  \"total_pages\": 0,\n  \"attendees\": &#91;]\n}<\/code><\/pre>\n\n\n\n<p>Since our REST API is built on top of the <a href=\"https:\/\/developer.wordpress.org\/rest-api\/\" target=\"_blank\" rel=\"noreferrer noopener\">WordPress API<\/a>, the same <a href=\"https:\/\/developer.wordpress.org\/rest-api\/using-the-rest-api\/authentication\/\" target=\"_blank\" rel=\"noreferrer noopener\">authentication practices<\/a> are baked in. This ensures that sensitive information (such as attendees&#8217; personal data) remains protected.<\/p>\n\n\n\n<p>You can refer to this article for more <a href=\"https:\/\/staging.theeventscalendar.com\/knowledgebase\/event-ticket-rest-api-basics\/\" target=\"_blank\" rel=\"noreferrer noopener\">basics on using our Event\/Ticket REST API<\/a>. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-set-up-an-authentication-method\">Set up an Authentication Method<\/h2>\n\n\n\n<p>Luckily, it is relatively simple to set up authentication methods for using the REST API within your site! Here are three options:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><a href=\"https:\/\/github.com\/WP-API\/Basic-Auth\" target=\"_blank\" rel=\"noreferrer noopener\">Basic Auth plugin<\/a>: <\/strong>Recommended for simple authentication. It allows you to use your WordPress admin credentials for API requests.<\/li>\n\n\n\n<li><strong><a href=\"https:\/\/wordpress.org\/plugins\/jwt-authentication-for-wp-rest-api\/\" target=\"_blank\" rel=\"noreferrer noopener\">JWT Authentication plugin<\/a>:<\/strong> For a more advanced JWT (JSON Web Token) authentication <\/li>\n\n\n\n<li><strong>Nonce Authentication<\/strong>: Create a nonce using the <code>wp_create_nonce()<\/code> function with the action set to <code>'wp_rest'<\/code>. This method requires more technical understanding but is very versatile once set up.<\/li>\n<\/ol>\n\n\n\n<p>Regardless of the option you choose here, you are basically setting up a secret password (token) within your site that will allow you to have access to otherwise protected information or actions. This authentication process is already happening &#8220;under the hood&#8221; on every page you visit when you are logged in to the admin side of your site, but for use in REST API calls this needs to be explicitly set up.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-make-a-rest-request-using-curl\">Make a REST Request using curl<\/h2>\n\n\n\n<p>Using the same example as before, how would we <code>GET<\/code> all the attendees from our site? For the purposes of this example, let&#8217;s assume you have the Basic Auth plugin installed and activated, which allows you to use your WP admin credentials for authentication using a <a href=\"https:\/\/curl.se\/docs\/httpscripting.html\" target=\"_blank\" rel=\"noreferrer noopener nofollow\"><code>curl<\/code> request<\/a>. In a terminal, you can then run a command like this (replacing <code>user:pass<\/code> with your WP username and password and update the URL):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl --user user:pass -X GET \\\n    -H \"Content-Type: application\/json\" \\\n    &#91;https:\/\/www.yoursite.url]\/wp-json\/tribe\/tickets\/v1\/attendees<\/code><\/pre>\n\n\n\n<p>Using a locally hosted site with one attendee as an example, this is what the response will look like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"rest_url\": \"https:\/\/stable.dev.lndo.site\/wp-json\/tribe\/tickets\/v1\/attendees\/\",\n  \"total\": 1,\n  \"total_pages\": 1,\n  \"attendees\": &#91;\n    {\n      \"id\": 33,\n      \"post_id\": 29,\n      \"ticket_id\": 30,\n      \"global_id\": \"stable.dev.lndo.site?type=attendee&amp;id=33\",\n      \"global_id_lineage\": &#91;\"stable.dev.lndo.site?type=attendee&amp;id=33\"],\n      \"author\": \"1\",\n      \"status\": \"publish\",\n      \"date\": \"2024-06-06 15:36:45\",\n      \"date_utc\": \"2024-06-06 21:36:45\",\n      \"modified\": \"2024-06-06 15:36:45\",\n      \"modified_utc\": \"2024-06-06 21:36:45\",\n      \"rest_url\": \"https:\/\/stable.dev.lndo.site\/wp-json\/tribe\/tickets\/v1\/attendees\/33\",\n      \"ticket\": {\n        \"id\": \"30\",\n        \"title\": \"Entree\",\n        \"description\": \"Allows entree to the BBQ\",\n        \"raw_price\": 20,\n        \"formatted_price\": \"20.00\",\n        \"currency_config\": {\n          \"symbol\": \"&amp;#36;\",\n          \"placement\": \"prefix\",\n          \"decimal_point\": \".\",\n          \"thousands_sep\": \",\",\n          \"number_of_decimals\": 2\n        },\n        \"start_sale\": \"2024-06-01\",\n        \"end_sale\": \"2024-07-01\"\n      },\n      \"title\": \"Steve Harvey\",\n      \"optout\": true,\n      \"provider\": \"woo\",\n      \"order\": \"32\",\n      \"sku\": \"\",\n      \"email\": \"steve@test.com\",\n      \"checked_in\": false,\n      \"checkin_details\": false,\n      \"is_subscribed\": false,\n      \"is_purchaser\": true,\n      \"payment\": {\n        \"provider\": \"woo\",\n        \"price\": 20,\n        \"currency\": \"$\",\n        \"date\": \"2024-06-06 15:36:16\",\n        \"date_details\": {\n          \"year\": \"2024\",\n          \"month\": \"06\",\n          \"day\": \"06\",\n          \"hour\": \"15\",\n          \"minutes\": \"36\",\n          \"seconds\": \"16\"\n        }\n      }\n    }\n  ]\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-making-a-rest-request-with-ajax\">Making a REST Request with AJAX<\/h2>\n\n\n\n<p>This section will assume that you know how to set up a basic AJAX call (but <a href=\"https:\/\/wpwebinfotech.com\/blog\/how-to-use-ajax-in-wordpress\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">here is a guide<\/a> if you need help getting started). <\/p>\n\n\n\n<p>In your PHP where you have the script <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_enqueue_script\/\" target=\"_blank\" rel=\"noreferrer noopener\">enqueued<\/a>, you can add a nonce as a localized variable with<a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_localize_script\/\" target=\"_blank\" rel=\"noreferrer noopener\"> <code>wp_localize_script()<\/code><\/a>: <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n\/\/ Localize the script with the nonce\nwp_localize_script( &#039;handle-of-script&#039;, &#039;localized_script_variables&#039;, array( \n    &#039;ajax_url&#039;      =&gt; admin_url( &#039;admin-ajax.php&#039; ),\n    &#039;rest_endpoint&#039; =&gt; &#039;\/wp-json\/tribe\/tickets\/v1\/attendees\/&#039;,\n    &#039;nonce&#039;         =&gt; wp_create_nonce( &#039;wp_rest&#039; ),\n) );\n<\/pre><\/div>\n\n\n<p>Then in your Javascript file, use the nonce to authenticate your request:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n$.ajax({\n    url: localized_script_variables.ajax_url,\n    type: &#039;GET&#039;,\n    dataType: &#039;json&#039;,\n    headers: {\n        &#039;X-WP-Nonce&#039;: localized_script_variables.nonce,\n    },\n    success: renderAttendees \/\/ This can be a callback that handles the attendees in the response.\n});\n<\/pre><\/div>\n\n\n<p>This ensures that your AJAX request is authenticated and securely retrieves the data you need. You can test that the authentication is happening by triggering the AJAX call as a logged-in user versus an incognito window. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>If you run into any issues, feel free to open a ticket with our support team. We\u2019re always here to help with the basics, but please keep in mind that our ability to assist with customizations might be a bit limited. Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some REST API requests require authentication to return information for a request &#8211; for example the endpoint: GET https:\/\/demo.theeventscalendar.com\/wp-json\/tribe\/tickets\/v1\/attendees is a valid endpoint to return all attendees for the given site, but without proper authentication, a site that has attendees will still return: Since our REST API is built on top of the WordPress API,&#8230;<\/p>\n","protected":false},"author":44,"featured_media":1955565,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_kad_blocks_custom_css":"","_kad_blocks_head_custom_js":"","_kad_blocks_body_custom_js":"","_kad_blocks_footer_custom_js":"","_swpsp_post_exclude":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"ep_exclude_from_search":false,"footnotes":""},"categories":[24,59],"tags":[225,25],"stellar-product-taxonomy":[155,156,161],"class_list":["post-1960612","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-customizing","category-customizing-resources","tag-api","tag-customizations","stellar-product-taxonomy-event-tickets","stellar-product-taxonomy-event-tickets-plus","stellar-product-taxonomy-the-events-calendar"],"acf":[],"taxonomy_info":{"category":[{"value":24,"label":"Customizations"},{"value":59,"label":"PHP &amp; Functions"}],"post_tag":[{"value":225,"label":"API"},{"value":25,"label":"Customizations"}],"stellar-product-taxonomy":[{"value":155,"label":"Event Tickets"},{"value":156,"label":"Event Tickets Plus"},{"value":161,"label":"The Events Calendar"}]},"featured_image_src_large":["https:\/\/images.theeventscalendar.com\/kb\/uploads\/2023\/02\/social-share-1024x538.png",1024,538,true],"author_info":{"display_name":"Sam Dokus","author_link":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/author\/sam-dokus\/"},"comment_info":0,"category_info":[{"term_id":24,"name":"Customizations","slug":"customizing","term_group":0,"term_taxonomy_id":24,"taxonomy":"category","description":"","parent":0,"count":110,"filter":"raw","term_order":"0","cat_ID":24,"category_count":110,"category_description":"","cat_name":"Customizations","category_nicename":"customizing","category_parent":0},{"term_id":59,"name":"PHP &amp; Functions","slug":"customizing-resources","term_group":0,"term_taxonomy_id":59,"taxonomy":"category","description":"","parent":24,"count":101,"filter":"raw","term_order":"0","cat_ID":59,"category_count":101,"category_description":"","cat_name":"PHP &amp; Functions","category_nicename":"customizing-resources","category_parent":24}],"tag_info":[{"term_id":225,"name":"API","slug":"api","term_group":0,"term_taxonomy_id":225,"taxonomy":"post_tag","description":"","parent":0,"count":5,"filter":"raw","term_order":"0"},{"term_id":25,"name":"Customizations","slug":"customizations","term_group":0,"term_taxonomy_id":25,"taxonomy":"post_tag","description":"","parent":0,"count":177,"filter":"raw","term_order":"0"}],"_links":{"self":[{"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/1960612","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/comments?post=1960612"}],"version-history":[{"count":20,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/1960612\/revisions"}],"predecessor-version":[{"id":1963266,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/1960612\/revisions\/1963266"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/media\/1955565"}],"wp:attachment":[{"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/media?parent=1960612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/categories?post=1960612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/tags?post=1960612"},{"taxonomy":"stellar-product-taxonomy","embeddable":true,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/stellar-product-taxonomy?post=1960612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}