Getting started with the search Spaces endpoint

This quick start guide will help you make your first request to the search Spaces endpoint with a set of specified fields using Postman.

If you would like to see sample code in different programming languages, please visit our Twitter API v2 sample code GitHub repository.

Prerequisites

For you to be able to complete this guide, you will have need to have a set of keys and tokens, which you can generate by following these steps:

     

Steps to build a search Spaces request

Step one: Start with a tool or library

There are several different tools, code examples, and libraries that you can use to make a request to this endpoint, but we are going to use the Postman tool here to simplify the process.

To load Twitter API v2 Postman collection into your environment, please click on the following button:

 

Once you have the Twitter API v2 collection loaded in Postman, navigate to the Spaces folder and find the "Search Spaces" request.

Step two: Authenticate your request

To properly make a request to the Twitter API, you need to verify that you have permission to do so. To do this with the Spaces lookup endpoints, you must authenticate your request. Using OAuth 2.0 Bearer Token is significantly easier, so we will describe how to use this method in this guide.

Step three: create a search query

This endpoint accepts text as a search query. Unlike other search endpoints, it does not accept operators, grouping, and logical operators. For this exercise, we will use “hello” as a simple query.

In Postman, navigate to the "Params" tab and enter this user ID into the "Value" column of the id parameter.

Key Value
query hello

 

Step four: Identify and specify which fields you would like to retrieve

If you click the "Send" button after step three, you will receive the ID of the Spaces and its state, which are the only Space object fields returned by default in your response.

If you would like to receive additional fields, you will have to specify them in your request with the space.fields or expansions parameters.

For this exercise, we will requested three additional sets of fields from different objects:

  • The additional title field in the primary Spaces object.
  • The full user object of the specified creator ID
  • The additional user.created_at field in the associated user object.

     

In Postman, navigate to the “Params” tab and add the following key:value pair to the “Query Params” table:

 

Key

Value

Returned fields

space.fields

title

creator_id

expansions

creator_id

includes.users.id, includes.users.name, includes.users.username

user.fields

created_at

includes.users.created_at

 

You should now see the following URL next to the “Send” button:

https://api.twitter.com/2/spaces/search?query=hello&space.fields=creator_id&expansions=creator_id&user.fields=created_at

 

Step five: Make your request and review your response

Once you have everything set up, hit the “Send” button and you will receive the following response:

      {
   "data": [
    {
        "creator_id": "2244994945",
        "id": "1zqKVXPQhvZJB",
        "title": "Hello world 👋",
        "state": "live"
   },
   "includes": {
       "users": [
           {
               "created_at": "2013-12-14T04:35:55.000Z",
               "name": "Twitter Dev",
               "id": "2244994945",
               "username": "TwitterDev"
           }
       ]
   }
]
}

    
Was this document helpful?
Thank you

Thank you for the feedback. We’re really glad we could help!

Thank you for the feedback. How could we improve this document?
Thank you for the feedback. Your comments will help us improve our documents in the future.